Skip to content

Commit

Permalink
Redirect if refresh failed (#143)
Browse files Browse the repository at this point in the history
* Redirect if refresh failed

* version bump

* Put back to 302

* back to 307
  • Loading branch information
PaulAsjes authored Nov 21, 2024
1 parent 8649e79 commit 3052db7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@workos-inc/authkit-nextjs",
"version": "0.16.0",
"version": "0.16.1",
"description": "Authentication and session helpers for using WorkOS & AuthKit with Next.js",
"sideEffects": false,
"type": "module",
Expand Down
20 changes: 15 additions & 5 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,22 @@ async function updateSession(
return response;
} catch (e) {
if (debug) console.log('Failed to refresh. Deleting cookie and redirecting.', e);
const response = NextResponse.next({
request: { headers: newRequestHeaders },
});
response.cookies.delete(cookieName);
return response;

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,
},
});
}

async function refreshSession(options: {
Expand Down
2 changes: 1 addition & 1 deletion src/workos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WorkOS } from '@workos-inc/node';
import { WORKOS_API_HOSTNAME, WORKOS_API_KEY, WORKOS_API_HTTPS, WORKOS_API_PORT } from './env-variables.js';

export const VERSION = '0.16.0';
export const VERSION = '0.16.1';

const options = {
apiHostname: WORKOS_API_HOSTNAME,
Expand Down

0 comments on commit 3052db7

Please sign in to comment.