diff --git a/package.json b/package.json index 7238c0b..c845c48 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/authkit-callback-route.ts b/src/authkit-callback-route.ts index 7593cbb..ba79384 100644 --- a/src/authkit-callback-route.ts +++ b/src/authkit-callback-route.ts @@ -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'); @@ -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' }, + }); } } diff --git a/src/session.ts b/src/session.ts index 05d7203..37f1b13 100644 --- a/src/session.ts +++ b/src/session.ts @@ -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 @@ -317,8 +326,9 @@ async function getSessionFromHeader(): Promise { 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)'.`, ); } diff --git a/src/workos.ts b/src/workos.ts index dfbb5d6..d689f6d 100644 --- a/src/workos.ts +++ b/src/workos.ts @@ -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,