Skip to content

Commit

Permalink
fix: update kindeAccessToken and kindeIdToken when refreshed
Browse files Browse the repository at this point in the history
Ensures we're passing the latest tokens to onSuccess if defined.
  • Loading branch information
Yoshify committed Dec 17, 2024
1 parent f5b4ca3 commit 1c28f9a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/authMiddleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
const resp = NextResponse.next();

// getAccessToken will validate the token
const kindeAccessToken = await getAccessToken(req);
let kindeAccessToken = await getAccessToken(req);

// if no access token, redirect to login
if (!kindeAccessToken) {
Expand All @@ -54,6 +54,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
try {
refreshResponse = await kindeClient.refreshTokens(session);
await session.setSessionItem("access_token", refreshResponse.access_token)
kindeAccessToken = refreshResponse.access_token

// if we want layouts/pages to get immediate access to the new token,
// we need to set the cookie on the response here
Expand All @@ -77,7 +78,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
}

// getIdToken will validate the token
const kindeIdToken = await getIdToken(req);
let kindeIdToken = await getIdToken(req);

// if no id token, redirect to login
if(!kindeIdToken) {
Expand All @@ -98,9 +99,9 @@ const handleMiddleware = async (req, options, onSuccess) => {
refreshResponse = await kindeClient.refreshTokens(session);
}


await session.setSessionItem("id_token", refreshResponse.id_token)

kindeIdToken = refreshResponse.id_token

// as above, if we want layouts/pages to get immediate access to the new token,
// we need to set the cookie on the response here
const splitCookies = getSplitCookies("id_token", refreshResponse.id_token)
Expand Down

0 comments on commit 1c28f9a

Please sign in to comment.