Files
medusa-store/packages/medusa/src/api/auth/utils/generate-jwt-token.ts
T
Adrien de Peretti d517dbd66a feat(): Add support for jwt asymetric keys (#12813)
* feat(): Add support for jwt asymetric keys

* Create early-chefs-chew.md

* fix unit tests

* Add verify options support

* feedback

* fix unit tests
2025-06-25 10:29:32 +02:00

45 lines
994 B
TypeScript

import {
AuthIdentityDTO,
ProjectConfigOptions,
} from "@medusajs/framework/types"
import { generateJwtToken } from "@medusajs/framework/utils"
import { type Secret } from "jsonwebtoken"
export function generateJwtTokenForAuthIdentity(
{
authIdentity,
actorType,
}: { authIdentity: AuthIdentityDTO; actorType: string },
{
secret,
expiresIn,
options,
}: {
secret: Secret
expiresIn: string | undefined
options?: ProjectConfigOptions["http"]["jwtOptions"]
}
) {
const expiresIn_ = expiresIn ?? options?.expiresIn
const entityIdKey = `${actorType}_id`
const entityId = authIdentity?.app_metadata?.[entityIdKey] as
| string
| undefined
return generateJwtToken(
{
actor_id: entityId ?? "",
actor_type: actorType,
auth_identity_id: authIdentity?.id ?? "",
app_metadata: {
[entityIdKey]: entityId,
},
},
{
secret,
expiresIn: expiresIn_,
jwtOptions: options,
}
)
}