@@ -7,6 +7,10 @@ import { cookies } from "next/headers";
77import type { NextRequest } from "next/server" ;
88import * as z from "zod" ;
99import { getSession } from "@/lib/auth" ;
10+ import {
11+ GOOGLE_OAUTH_CODE_VERIFIER_COOKIE_NAME ,
12+ GOOGLE_OAUTH_STATE_COOKIE_NAME ,
13+ } from "@/lib/cookies" ;
1014import { google } from "@/lib/oauth" ;
1115import {
1216 createSession ,
@@ -28,8 +32,27 @@ export async function GET(req: NextRequest) {
2832
2933 const cookieStore = await cookies ( ) ;
3034
31- const storedState = cookieStore . get ( "google_oauth_state" ) ?. value ?? null ;
32- const codeVerifier = cookieStore . get ( "google_code_verifier" ) ?. value ?? null ;
35+ const storedState =
36+ cookieStore . get ( GOOGLE_OAUTH_STATE_COOKIE_NAME ) ?. value ?? null ;
37+ const codeVerifier =
38+ cookieStore . get ( GOOGLE_OAUTH_CODE_VERIFIER_COOKIE_NAME ) ?. value ?? null ;
39+
40+ const clearOauthCookies = ( ) => {
41+ cookieStore . set ( GOOGLE_OAUTH_STATE_COOKIE_NAME , "" , {
42+ path : "/" ,
43+ httpOnly : true ,
44+ secure : process . env . NODE_ENV === "production" ,
45+ maxAge : 0 ,
46+ sameSite : "lax" ,
47+ } ) ;
48+ cookieStore . set ( GOOGLE_OAUTH_CODE_VERIFIER_COOKIE_NAME , "" , {
49+ path : "/" ,
50+ httpOnly : true ,
51+ secure : process . env . NODE_ENV === "production" ,
52+ maxAge : 0 ,
53+ sameSite : "lax" ,
54+ } ) ;
55+ } ;
3356
3457 if (
3558 code === null ||
@@ -42,6 +65,8 @@ export async function GET(req: NextRequest) {
4265 } ) ;
4366 }
4467
68+ clearOauthCookies ( ) ;
69+
4570 if ( state !== storedState ) {
4671 return new Response ( null , {
4772 status : 400 ,
0 commit comments