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
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
import jwt from "jsonwebtoken"
|
||||
import jwt, { type Secret, type SignOptions } from "jsonwebtoken"
|
||||
import { MedusaError } from "../common"
|
||||
|
||||
export const generateJwtToken = (
|
||||
tokenPayload: Record<string, unknown>,
|
||||
jwtConfig: {
|
||||
secret: string | undefined
|
||||
expiresIn: string | undefined
|
||||
secret?: Secret
|
||||
expiresIn?: number | string
|
||||
jwtOptions?: SignOptions
|
||||
}
|
||||
) => {
|
||||
if (!jwtConfig.secret || !jwtConfig.expiresIn) {
|
||||
if (
|
||||
!jwtConfig.secret ||
|
||||
(!jwtConfig.expiresIn && !jwtConfig.jwtOptions?.expiresIn)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
"JWT secret and expiresIn must be provided when generating a token"
|
||||
)
|
||||
}
|
||||
|
||||
const expiresIn = jwtConfig.expiresIn ?? jwtConfig.jwtOptions?.expiresIn
|
||||
return jwt.sign(tokenPayload, jwtConfig.secret, {
|
||||
expiresIn: jwtConfig.expiresIn,
|
||||
...jwtConfig.jwtOptions,
|
||||
expiresIn,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user