Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/authkit-callback-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion src/authkit-callback-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function handleAuth(options: HandleAuthOptions = {}) {
await saveSession({ accessToken, refreshToken, user, impersonator }, request);

if (onSuccess) {
await onSuccess({
const onSuccessResponse = await onSuccess({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I wonder if it makes sense to provide the default response as an argument to onSuccess? That way, you don't always need to create a new one from scratch.

This would also help make it more explicit that if onSuccess returns a NextResponse, it's going to take precedence over returnPathname if both are provided.

accessToken,
refreshToken,
user,
Expand All @@ -106,6 +106,10 @@ export function handleAuth(options: HandleAuthOptions = {}) {
organizationId,
state: customState,
});

if (onSuccessResponse) {
return onSuccessResponse;
}
}

return response;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { NextResponse, type NextRequest } from 'next/server';
import type { NextResponse, NextRequest } from 'next/server';


export interface HandleAuthOptions {
returnPathname?: string;
baseURL?: string;
onSuccess?: (data: HandleAuthSuccessData) => void | Promise<void>;
onSuccess?: (data: HandleAuthSuccessData) => void | Promise<void> | Promise<NextResponse>;
Copy link
Member

@nicknisi nicknisi Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I don't think it should be required for this to be async to return a NextResponse.

Suggested change
onSuccess?: (data: HandleAuthSuccessData) => void | Promise<void> | Promise<NextResponse>;
onSuccess?: (data: HandleAuthSuccessData) => void | NextResponse | Promise<void> | Promise<NextResponse>;

onError?: (params: { error?: unknown; request: NextRequest }) => Response | Promise<Response>;
}

Expand Down