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:
Adrien de Peretti
2025-06-25 10:29:32 +02:00
committed by GitHub
parent a833c3c98c
commit d517dbd66a
23 changed files with 813 additions and 43 deletions
+11 -5
View File
@@ -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,
})
}