From ea799dc1d7e890f0e7433170c7e7dd04cebbad31 Mon Sep 17 00:00:00 2001 From: axlEscalada Date: Tue, 14 Oct 2025 15:29:59 -0300 Subject: [PATCH 1/3] feat: allow dynamic redirect on success --- src/authkit-callback-route.spec.ts | 16 ++++++++++++++++ src/authkit-callback-route.ts | 6 +++++- src/interfaces.ts | 4 ++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/authkit-callback-route.spec.ts b/src/authkit-callback-route.spec.ts index 665fc42..ebc8bed 100644 --- a/src/authkit-callback-route.spec.ts +++ b/src/authkit-callback-route.spec.ts @@ -276,6 +276,22 @@ describe('authkit-callback-route', () => { expect(session?.accessToken).toBe(newAccessToken); }); + it('should allow onSuccess redirect using NextResponse', async () => { + jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); + + // Set up request with code + request.nextUrl.searchParams.set('code', 'test-code'); + + const handler = handleAuth({ + onSuccess: async (data) => { + return NextResponse.redirect('https://example.com/dashboard'); + }, + }); + + const response = await handler(request); + expect(response.headers.get('Location')).toBe('https://example.com/dashboard'); + }); + it('should pass custom state data to onSuccess callback', async () => { jest.mocked(workos.userManagement.authenticateWithCode).mockResolvedValue(mockAuthResponse); diff --git a/src/authkit-callback-route.ts b/src/authkit-callback-route.ts index e826a9f..ab80567 100644 --- a/src/authkit-callback-route.ts +++ b/src/authkit-callback-route.ts @@ -96,7 +96,7 @@ export function handleAuth(options: HandleAuthOptions = {}) { await saveSession({ accessToken, refreshToken, user, impersonator }, request); if (onSuccess) { - await onSuccess({ + const redirect = await onSuccess({ accessToken, refreshToken, user, @@ -106,6 +106,10 @@ export function handleAuth(options: HandleAuthOptions = {}) { organizationId, state: customState, }); + + if (redirect) { + return redirect; + } } return response; diff --git a/src/interfaces.ts b/src/interfaces.ts index d3f6ecd..197d51e 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,10 +1,10 @@ import type { AuthenticationResponse, OauthTokens, User } from '@workos-inc/node'; -import { type NextRequest } from 'next/server'; +import { NextResponse, type NextRequest } from 'next/server'; export interface HandleAuthOptions { returnPathname?: string; baseURL?: string; - onSuccess?: (data: HandleAuthSuccessData) => void | Promise; + onSuccess?: (data: HandleAuthSuccessData) => void | Promise | Promise; onError?: (params: { error?: unknown; request: NextRequest }) => Response | Promise; } From 73cc82ac574d644af6580136495f9bf958a73616 Mon Sep 17 00:00:00 2001 From: axlEscalada Date: Tue, 14 Oct 2025 16:00:28 -0300 Subject: [PATCH 2/3] feat: use a better name for on success response since we allow NextResponse and not just redirects --- src/authkit-callback-route.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/authkit-callback-route.ts b/src/authkit-callback-route.ts index ab80567..8c2a7d4 100644 --- a/src/authkit-callback-route.ts +++ b/src/authkit-callback-route.ts @@ -96,7 +96,7 @@ export function handleAuth(options: HandleAuthOptions = {}) { await saveSession({ accessToken, refreshToken, user, impersonator }, request); if (onSuccess) { - const redirect = await onSuccess({ + const onSuccessResponse = await onSuccess({ accessToken, refreshToken, user, @@ -107,8 +107,8 @@ export function handleAuth(options: HandleAuthOptions = {}) { state: customState, }); - if (redirect) { - return redirect; + if (onSuccessResponse) { + return onSuccessResponse; } } From 1500614de0e6deec1f7670dd381f1672f21fda64 Mon Sep 17 00:00:00 2001 From: axlEscalada Date: Tue, 14 Oct 2025 16:41:26 -0300 Subject: [PATCH 3/3] feat: run prettier --- src/authkit-callback-route.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/authkit-callback-route.spec.ts b/src/authkit-callback-route.spec.ts index ebc8bed..aae887b 100644 --- a/src/authkit-callback-route.spec.ts +++ b/src/authkit-callback-route.spec.ts @@ -284,7 +284,7 @@ describe('authkit-callback-route', () => { const handler = handleAuth({ onSuccess: async (data) => { - return NextResponse.redirect('https://example.com/dashboard'); + return NextResponse.redirect('https://example.com/dashboard'); }, });