Skip to content

Commit d673602

Browse files
test github auth
1 parent 2b4b583 commit d673602

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
2-
import { revalidatePath } from 'next/cache';
32
import { cookies } from 'next/headers';
43
import { NextResponse } from 'next/server';
54

5+
const isDevelopment = process.env.NODE_ENV === 'development';
6+
67
export async function GET(request: Request) {
78
const requestUrl = new URL(request.url);
89
const code = requestUrl.searchParams.get('code');
910
const next = requestUrl.searchParams.get('next');
11+
const provider = requestUrl.searchParams.get('provider');
1012

1113
if (code) {
1214
const supabase = createRouteHandlerClient({ cookies });
@@ -19,7 +21,33 @@ export async function GET(request: Request) {
1921
// Potentially return an error response here
2022
}
2123
}
22-
revalidatePath('/', 'layout');
24+
25+
if (provider) {
26+
// HACK_ALERT!!!
27+
// cookie is probably set on 'next.digger.dev' we have to change it to `.digger.dev`
28+
const cookieKey = `sb-${process.env.SUPABASE_PROJECT_REF}-auth-token`;
29+
const cookieValue = cookies().get(cookieKey)?.value;
30+
const cookieStore = cookies();
31+
const currentCookieValue = cookieStore.get(cookieKey)?.value;
32+
// get domain of current reques
33+
const domain = new URL(request.url).hostname;
34+
if (
35+
domain.includes('next.digger.dev') &&
36+
currentCookieValue &&
37+
!isDevelopment
38+
) {
39+
// delete cookie from next.digger.dev
40+
cookieStore.delete(cookieKey);
41+
// set cookie to .digger.dev
42+
cookieStore.set(cookieKey, currentCookieValue, {
43+
domain: '.digger.dev',
44+
secure: true,
45+
path: '/',
46+
sameSite: 'lax',
47+
httpOnly: true,
48+
});
49+
}
50+
}
2351

2452
let redirectTo = new URL('/dashboard', requestUrl.origin);
2553

src/data/auth/auth.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ export const signInWithMagicLink = async (
6868
export const signInWithProvider = async (
6969
provider: AuthProvider,
7070
next?: string,
71-
): Promise<SAPayload<{
72-
url: string;
73-
}>> => {
71+
): Promise<
72+
SAPayload<{
73+
url: string;
74+
}>
75+
> => {
7476
const supabase = createSupabaseUserServerActionClient();
75-
const redirectToURL = new URL(toSiteURL('/auth/callback'));
77+
const redirectToURL = new URL(
78+
toSiteURL(`/auth/callback?provider=${provider}`),
79+
);
7680
if (next) {
7781
redirectToURL.searchParams.set('next', next);
7882
}
@@ -83,7 +87,6 @@ export const signInWithProvider = async (
8387
},
8488
});
8589

86-
8790
if (error) {
8891
return { status: 'error', message: error.message };
8992
}
@@ -93,8 +96,8 @@ export const signInWithProvider = async (
9396
return {
9497
status: 'success',
9598
data: {
96-
url: providerUrl
97-
}
99+
url: providerUrl,
100+
},
98101
};
99102
};
100103

0 commit comments

Comments
 (0)