From eecea125ee45df598f4f8796e758bfb7167173ae Mon Sep 17 00:00:00 2001 From: gf-rog Date: Fri, 22 Mar 2024 11:15:56 +0100 Subject: [PATCH] Fix error if no issuer found in token --- backend/src/misc/jwt.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/misc/jwt.ts b/backend/src/misc/jwt.ts index 395b158b..24d52d79 100644 --- a/backend/src/misc/jwt.ts +++ b/backend/src/misc/jwt.ts @@ -17,7 +17,9 @@ const issuers: Record = { }; export function generateAccessToken(userId: string) { - return jwt.sign({ iss: issuers["rest"], userId }, process.env.TOKEN_SECRET!, { expiresIn: 900 }); + return jwt.sign({ iss: issuers["rest"], userId }, process.env.TOKEN_SECRET!, { + expiresIn: 900, + }); } export function generateRefreshToken(userId: string) { @@ -50,7 +52,10 @@ export async function verifyKeycloakToken(tokenStr: string): Promise { export async function checkToken( tokenStr: string, ): Promise { - const tokenDecoded = jwt.decode(tokenStr) as TokenPayload; + const tokenDecoded = jwt.decode(tokenStr) as TokenPayload | null; + if (!tokenDecoded) { + return null; + } const issuer = tokenIssuerToName(tokenDecoded.iss || "");