Skip to content

Commit

Permalink
Allow specifying expiry when generating a JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythicaeda committed Jun 27, 2024
1 parent 58d2d17 commit a64a43c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/packages/auth/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ export function decodeJwt(authorizationHeader: string | undefined): JwtDecode {
}
}

export function generateJwt(username: string, defaultRole: string, allowedRoles: string[]): string | null {
export function generateJwt(
username: string,
defaultRole: string,
allowedRoles: string[],
expiry: string = getEnv().JWT_EXPIRATION,
): string | null {
try {
const { HASURA_GRAPHQL_JWT_SECRET, JWT_EXPIRATION } = getEnv();
const { HASURA_GRAPHQL_JWT_SECRET } = getEnv();
const { key, type }: JwtSecret = JSON.parse(HASURA_GRAPHQL_JWT_SECRET);
const options: jwt.SignOptions = { algorithm: type as Algorithm, expiresIn: JWT_EXPIRATION };
const options: jwt.SignOptions = { algorithm: type as Algorithm, expiresIn: expiry };
const payload: JwtPayload = {
'https://hasura.io/jwt/claims': {
'x-hasura-allowed-roles': allowedRoles,
Expand Down

0 comments on commit a64a43c

Please sign in to comment.