From d67360237cb4a59ba8bab90e40338f959dcdb429 Mon Sep 17 00:00:00 2001 From: psiddharthdesign <107192927+psiddharthdesign@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:36:33 +0530 Subject: [PATCH] test github auth --- .../(login-pages)/auth/callback/route.ts | 32 +++++++++++++++++-- src/data/auth/auth.ts | 17 ++++++---- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts b/src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts index 933d2f61..d7670917 100644 --- a/src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts +++ b/src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts @@ -1,12 +1,14 @@ import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'; -import { revalidatePath } from 'next/cache'; import { cookies } from 'next/headers'; import { NextResponse } from 'next/server'; +const isDevelopment = process.env.NODE_ENV === 'development'; + export async function GET(request: Request) { const requestUrl = new URL(request.url); const code = requestUrl.searchParams.get('code'); const next = requestUrl.searchParams.get('next'); + const provider = requestUrl.searchParams.get('provider'); if (code) { const supabase = createRouteHandlerClient({ cookies }); @@ -19,7 +21,33 @@ export async function GET(request: Request) { // Potentially return an error response here } } - revalidatePath('/', 'layout'); + + if (provider) { + // HACK_ALERT!!! + // cookie is probably set on 'next.digger.dev' we have to change it to `.digger.dev` + const cookieKey = `sb-${process.env.SUPABASE_PROJECT_REF}-auth-token`; + const cookieValue = cookies().get(cookieKey)?.value; + const cookieStore = cookies(); + const currentCookieValue = cookieStore.get(cookieKey)?.value; + // get domain of current reques + const domain = new URL(request.url).hostname; + if ( + domain.includes('next.digger.dev') && + currentCookieValue && + !isDevelopment + ) { + // delete cookie from next.digger.dev + cookieStore.delete(cookieKey); + // set cookie to .digger.dev + cookieStore.set(cookieKey, currentCookieValue, { + domain: '.digger.dev', + secure: true, + path: '/', + sameSite: 'lax', + httpOnly: true, + }); + } + } let redirectTo = new URL('/dashboard', requestUrl.origin); diff --git a/src/data/auth/auth.ts b/src/data/auth/auth.ts index e225e370..e8bf6c9c 100644 --- a/src/data/auth/auth.ts +++ b/src/data/auth/auth.ts @@ -68,11 +68,15 @@ export const signInWithMagicLink = async ( export const signInWithProvider = async ( provider: AuthProvider, next?: string, -): Promise> => { +): Promise< + SAPayload<{ + url: string; + }> +> => { const supabase = createSupabaseUserServerActionClient(); - const redirectToURL = new URL(toSiteURL('/auth/callback')); + const redirectToURL = new URL( + toSiteURL(`/auth/callback?provider=${provider}`), + ); if (next) { redirectToURL.searchParams.set('next', next); } @@ -83,7 +87,6 @@ export const signInWithProvider = async ( }, }); - if (error) { return { status: 'error', message: error.message }; } @@ -93,8 +96,8 @@ export const signInWithProvider = async ( return { status: 'success', data: { - url: providerUrl - } + url: providerUrl, + }, }; };