Skip to content

Commit

Permalink
fix: wrap token decoding in middleware in try/catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshify committed Dec 18, 2024
1 parent d29222c commit 7e8816f
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/authMiddleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,34 @@ const handleMiddleware = async (req, options, onSuccess) => {
}
}

const accessTokenValue = jwtDecoder<KindeAccessToken>(
kindeAccessToken,
);
let accessTokenValue: KindeAccessToken | null = null
let idTokenValue: KindeIdToken | null = null

const idTokenValue = jwtDecoder<KindeIdToken>(
kindeIdToken,
);
try {
accessTokenValue = jwtDecoder<KindeAccessToken>(
kindeAccessToken,
);
} catch(error) {
if(config.isDebugMode) {
console.error('authMiddleware: access token decode failed, redirecting to login')
}
return NextResponse.redirect(
new URL(loginRedirectUrl, options?.redirectURLBase || config.redirectURL),
);
}

try {
idTokenValue = jwtDecoder<KindeIdToken>(
kindeIdToken,
);
} catch(error) {
if(config.isDebugMode) {
console.error('authMiddleware: id token decode failed, redirecting to login')
}
return NextResponse.redirect(
new URL(loginRedirectUrl, options?.redirectURLBase || config.redirectURL),
);
}

const customValidationValid = options?.isAuthorized
? options.isAuthorized({ req, token: accessTokenValue })
Expand Down

0 comments on commit 7e8816f

Please sign in to comment.