Skip to content

Commit

Permalink
Only redirect in middleware auth mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAsjes committed Dec 23, 2024
1 parent 565f393 commit a087499
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,20 @@ async function updateSession(
nextCookies.delete(cookieName);
}

// If we get here, the session is invalid and the user needs to sign in again.
// We redirect to the current URL which will trigger the middleware again.
// This is outside of the above block because you cannot redirect in Next.js
// from inside a try/catch block.
return NextResponse?.redirect
? NextResponse.redirect(request.url)
: new Response(null, {
status: 307,
headers: {
Location: request.url,
},
});
if (middlewareAuth.enabled) {
// If we get here, the session is invalid and the user needs to sign in again because we're using middleware auth mode.
// We redirect to the current URL which will trigger the middleware again.
// This is outside of the above block because you cannot redirect in Next.js
// from inside a try/catch block.
return NextResponse?.redirect
? NextResponse.redirect(request.url)
: new Response(null, {
status: 307,
headers: {
Location: request.url,
},
});
}
}

async function refreshSession(options: {
Expand Down

0 comments on commit a087499

Please sign in to comment.