Skip to main content

Authentication API Endpoints

Authentication endpoints handle NextAuth.js route handling, password management, and current user session retrieval. The core NextAuth catch-all route manages all OAuth callbacks, session management, and CSRF protection automatically.

NextAuth Handler (/api/auth/[...nextauth])

The catch-all route exports NextAuth's handlers from lib/auth/index.ts:

import { handlers } from '@/lib/auth';

export const { GET, POST } = handlers;

This single route handles all NextAuth operations:

GET Endpoints (via NextAuth)

PathDescription
/api/auth/signinRender sign-in page or redirect to provider
/api/auth/signoutHandle sign-out
/api/auth/sessionGet current session as JSON
/api/auth/csrfGet CSRF token
/api/auth/providersList available auth providers
/api/auth/callback/[provider]OAuth callback handler

POST Endpoints (via NextAuth)

PathDescription
/api/auth/signin/[provider]Initiate sign-in with provider
/api/auth/signoutProcess sign-out
/api/auth/callback/credentialsProcess credentials sign-in
/api/auth/_logAuth.js internal logging

OAuth Callback Flow

When a user authenticates with an OAuth provider:

1. User clicks "Sign in with Google"
2. Redirect to Google consent screen
3. Google redirects back to /api/auth/callback/google
4. NextAuth verifies the OAuth code
5. signIn callback runs (lib/auth/index.ts)
-> Validates user email
-> Allows account linking for OAuth
6. jwt callback enriches token
-> Sets userId, provider, isAdmin
-> Creates client profile for new OAuth users
7. Session created, user redirected to callback URL

Custom Pages

NextAuth is configured to use custom authentication pages rather than the default NextAuth UI:

PurposeCustom Path
Sign In/auth/signin
Sign Out/auth/signout
Error/auth/error
Verify Request/auth/verify-request
New User Registration/auth/register

Password Management (/api/auth/change-password)

MethodPathDescription
POST/api/auth/change-passwordChange authenticated user's password

Request Body

{
"currentPassword": "old-password",
"newPassword": "new-secure-password"
}

Authentication

Requires a valid session. The endpoint verifies the current password before updating.

Response

// Success
{ "success": true, "message": "Password changed successfully" }

// Error
{ "success": false, "error": "Current password is incorrect" }

Current User (/api/current-user)

MethodPathDescription
GET/api/current-userGet current authenticated user data

Response

Returns the session user object enriched with application-specific fields:

{
"user": {
"id": "user-uuid",
"email": "user@example.com",
"name": "John Doe",
"image": "https://...",
"isAdmin": false,
"clientProfileId": "profile-uuid",
"provider": "google"
}
}

Unauthenticated Response

Returns null or a 401 status when no valid session exists.

Session Token Handling

NextAuth stores session tokens in HTTP-only cookies:

Cookie NameEnvironment
next-auth.session-tokenDevelopment (HTTP)
__Secure-next-auth.session-tokenProduction (HTTPS)

CSRF Protection

NextAuth includes built-in CSRF protection. A CSRF token cookie (next-auth.csrf-token) is set on the client and must be included with POST requests to NextAuth endpoints.

Error Handling

Authentication errors are mapped to user-friendly messages in lib/auth/error-handler.ts:

Error PatternUser Message
GOOGLE_CLIENT_ID relatedGoogle authentication is not properly configured
GITHUB_CLIENT_ID relatedGitHub authentication is not properly configured
FB_CLIENT_ID relatedFacebook authentication is not properly configured
MICROSOFT_CLIENT_ID relatedMicrosoft authentication is not properly configured
SUPABASE relatedSupabase authentication is not properly configured
NEXTAUTH relatedNextAuth is not properly configured

The handleAuthError() function catches these errors and returns a structured { error: string } response.

Auth Events

The NextAuth configuration in lib/auth/index.ts handles lifecycle events:

Sign-Out Event

Invalidates the session cache for the user to ensure stale session data is not served:

events: {
signOut: async (event) => {
const token = 'token' in event ? event.token : undefined;
if (token?.userId) {
await invalidateSessionCache(undefined, token.userId);
}
}
}

User Update Event

Invalidates the session cache when user data changes (e.g., profile update, role change):

events: {
updateUser: async ({ user }) => {
if (user?.id) {
await invalidateSessionCache(undefined, user.id);
}
}
}
FilePurpose
auth.config.tsTop-level provider configuration
lib/auth/index.tsNextAuth instance with callbacks and events
lib/auth/providers.tsOAuth provider factory
lib/auth/credentials.tsEmail/password provider
lib/auth/cached-session.tsSession caching layer
lib/auth/admin-guard.tsAdmin route middleware