* feat(): Add support for jwt asymetric keys * Create early-chefs-chew.md * fix unit tests * Add verify options support * feedback * fix unit tests
45 lines
994 B
TypeScript
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,
|
|
}
|
|
)
|
|
}
|