Skip to content

Commit

Permalink
Support Next 13 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAsjes authored Oct 28, 2024
1 parent 7b5324a commit 61e0b0d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@workos-inc/authkit-nextjs",
"version": "0.13.0",
"version": "0.13.1",
"description": "Authentication and session helpers for using WorkOS & AuthKit with Next.js",
"sideEffects": false,
"type": "module",
Expand Down Expand Up @@ -38,7 +38,7 @@
"eslint": "^8.29.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-require-extensions": "^0.1.3",
"next": "^14.1.3",
"next": "^15.0.1",
"prettier": "^3.3.3",
"typescript": "5.4.2",
"typescript-eslint": "^7.2.0"
Expand Down
33 changes: 24 additions & 9 deletions src/authkit-callback-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ export function handleAuth(options: HandleAuthOptions = {}) {
url.pathname = returnPathname;
}

const response = NextResponse.redirect(url);
// Fall back to standard Response if NextResponse is not available.
// This is to support Next.js 13.
const response = NextResponse?.redirect
? NextResponse.redirect(url)
: new Response(null, {
status: 302,
headers: {
Location: url.toString(),
},
});

if (!accessToken || !refreshToken) throw new Error('response is missing tokens');

Expand Down Expand Up @@ -71,14 +80,20 @@ export function handleAuth(options: HandleAuthOptions = {}) {
};

function errorResponse() {
return NextResponse.json(
{
error: {
message: 'Something went wrong',
description: 'Couldn’t sign in. If you are not sure what happened, please contact your organization admin.',
},
const errorBody = {
error: {
message: 'Something went wrong',
description: "Couldn't sign in. If you are not sure what happened, please contact your organization admin.",
},
{ status: 500 },
);
};

// Use NextResponse if available, fallback to standard Response
// This is to support Next.js 13.
return NextResponse?.json
? NextResponse.json(errorBody, { status: 500 })
: new Response(JSON.stringify(errorBody), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
24 changes: 17 additions & 7 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@ async function updateSession(
if (middlewareAuth.enabled && matchedPaths.length === 0 && !session) {
if (debug) console.log(`Unauthenticated user on protected route ${request.url}, redirecting to AuthKit`);

return NextResponse.redirect(
await getAuthorizationUrl({
returnPathname: getReturnPathname(request.url),
redirectUri: redirectUri ?? WORKOS_REDIRECT_URI,
}),
);
const redirectTo = await getAuthorizationUrl({
returnPathname: getReturnPathname(request.url),
redirectUri: redirectUri ?? WORKOS_REDIRECT_URI,
});

// Fall back to standard Response if NextResponse is not available.
// This is to support Next.js 13.
return NextResponse?.redirect
? NextResponse.redirect(redirectTo)
: new Response(null, {
status: 302,
headers: {
Location: redirectTo,
},
});
}

// If no session, just continue
Expand Down Expand Up @@ -317,8 +326,9 @@ async function getSessionFromHeader(): Promise<Session | undefined> {
const hasMiddleware = Boolean(headersList.get(middlewareHeaderName));

if (!hasMiddleware) {
const url = headersList.get('x-url');
throw new Error(
"You are calling 'withAuth' on a path that isn’t covered by the AuthKit middleware. Make sure it is running on all paths you are calling withAuth from by updating your middleware config in `middleware.(js|ts)`.",
`You are calling 'withAuth' on ${url} that isn’t covered by the AuthKit middleware. Make sure it is running on all paths you are calling 'withAuth' from by updating your middleware config in 'middleware.(js|ts)'.`,
);
}

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.13.0';
export const VERSION = '0.13.1';

const options = {
apiHostname: WORKOS_API_HOSTNAME,
Expand Down

0 comments on commit 61e0b0d

Please sign in to comment.