1+ import { clearOAuthState , exchangeCodeForToken , getOAuthState , OAUTH_CONFIG , setAuthToken } from "@/lib/auth" ;
12import { NextRequest , NextResponse } from "next/server" ;
2- import {
3- clearOAuthState ,
4- exchangeCodeForToken ,
5- getOAuthState ,
6- OAUTH_CONFIG ,
7- setAuthToken ,
8- } from "@/lib/auth" ;
93
104/**
115 * OAuth 2.0 Authorization Code Flow Callback Handler
12- *
6+ *
137 * This endpoint handles the callback from the OAuth authorization server
148 * according to RFC 6749 and RFC 7636 (PKCE).
15- *
9+ *
1610 * Expected query parameters:
1711 * - code: Authorization code from the OAuth server
1812 * - state: State parameter for CSRF protection
@@ -29,49 +23,49 @@ export async function GET(request: NextRequest) {
2923 // Handle OAuth errors
3024 if ( error ) {
3125 console . error ( "OAuth authorization error:" , error , errorDescription ) ;
32-
26+
3327 const errorUrl = new URL ( "/login" , request . url ) ;
3428 errorUrl . searchParams . set ( "error" , error ) ;
3529 if ( errorDescription ) {
3630 errorUrl . searchParams . set ( "error_description" , errorDescription ) ;
3731 }
38-
32+
3933 return NextResponse . redirect ( errorUrl ) ;
4034 }
4135
4236 // Validate required parameters
4337 if ( ! code || ! state ) {
4438 console . error ( "Missing required parameters in OAuth callback" ) ;
45-
39+
4640 const errorUrl = new URL ( "/login" , request . url ) ;
4741 errorUrl . searchParams . set ( "error" , "invalid_request" ) ;
4842 errorUrl . searchParams . set ( "error_description" , "Missing required parameters" ) ;
49-
43+
5044 return NextResponse . redirect ( errorUrl ) ;
5145 }
5246
5347 try {
5448 // Retrieve and validate stored OAuth state
5549 const { state : storedState , codeVerifier } = await getOAuthState ( ) ;
56-
50+
5751 if ( ! storedState || ! codeVerifier ) {
5852 console . error ( "Missing OAuth state or code verifier" ) ;
59-
53+
6054 const errorUrl = new URL ( "/login" , request . url ) ;
6155 errorUrl . searchParams . set ( "error" , "invalid_request" ) ;
6256 errorUrl . searchParams . set ( "error_description" , "OAuth state not found" ) ;
63-
57+
6458 return NextResponse . redirect ( errorUrl ) ;
6559 }
6660
6761 // Validate state parameter (CSRF protection)
6862 if ( state !== storedState ) {
6963 console . error ( "OAuth state mismatch" , { state, storedState } ) ;
70-
64+
7165 const errorUrl = new URL ( "/login" , request . url ) ;
7266 errorUrl . searchParams . set ( "error" , "invalid_request" ) ;
7367 errorUrl . searchParams . set ( "error_description" , "Invalid state parameter" ) ;
74-
68+
7569 return NextResponse . redirect ( errorUrl ) ;
7670 }
7771
@@ -81,9 +75,9 @@ export async function GET(request: NextRequest) {
8175
8276 // Store the access token in encrypted session
8377 await setAuthToken (
84- tokenResponse . access_token ,
85- tokenResponse . token_type ,
86- tokenResponse . expires_in
78+ tokenResponse . access_token ,
79+ tokenResponse . token_type ,
80+ tokenResponse . expires_in ,
8781 ) ;
8882
8983 // Clear OAuth state cookies
@@ -92,14 +86,13 @@ export async function GET(request: NextRequest) {
9286 // Redirect to the main application
9387 const successUrl = new URL ( "/" , request . url ) ;
9488 return NextResponse . redirect ( successUrl ) ;
95-
9689 } catch ( error ) {
9790 console . error ( "Token exchange failed:" , error ) ;
98-
91+
9992 const errorUrl = new URL ( "/login" , request . url ) ;
10093 errorUrl . searchParams . set ( "error" , "server_error" ) ;
10194 errorUrl . searchParams . set ( "error_description" , "Failed to exchange authorization code" ) ;
102-
95+
10396 return NextResponse . redirect ( errorUrl ) ;
10497 }
10598}
0 commit comments