Skip to content

Commit

Permalink
test github auth
Browse files Browse the repository at this point in the history
  • Loading branch information
psiddharthdesign committed Jul 22, 2024
1 parent 2b4b583 commit d673602
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
32 changes: 30 additions & 2 deletions src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand All @@ -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);

Expand Down
17 changes: 10 additions & 7 deletions src/data/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ export const signInWithMagicLink = async (
export const signInWithProvider = async (
provider: AuthProvider,
next?: string,
): Promise<SAPayload<{
url: string;
}>> => {
): 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);
}
Expand All @@ -83,7 +87,6 @@ export const signInWithProvider = async (
},
});


if (error) {
return { status: 'error', message: error.message };
}
Expand All @@ -93,8 +96,8 @@ export const signInWithProvider = async (
return {
status: 'success',
data: {
url: providerUrl
}
url: providerUrl,
},
};
};

Expand Down

0 comments on commit d673602

Please sign in to comment.