Eren-Sama commited on
Commit
aaa2d62
·
1 Parent(s): cc43351

Fix Google OAuth: add /auth/callback route, redirect to / instead of /login

Browse files
frontend/src/app/auth/callback/route.ts ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createServerClient } from "@supabase/ssr";
2
+ import { cookies } from "next/headers";
3
+ import { NextResponse, type NextRequest } from "next/server";
4
+
5
+ export async function GET(request: NextRequest) {
6
+ const { searchParams, origin } = new URL(request.url);
7
+ const code = searchParams.get("code");
8
+ const next = searchParams.get("next") ?? "/dashboard";
9
+
10
+ if (code) {
11
+ const cookieStore = await cookies();
12
+ const supabase = createServerClient(
13
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
14
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
15
+ {
16
+ cookies: {
17
+ getAll() {
18
+ return cookieStore.getAll();
19
+ },
20
+ setAll(cookiesToSet) {
21
+ cookiesToSet.forEach(({ name, value, options }) =>
22
+ cookieStore.set(name, value, options)
23
+ );
24
+ },
25
+ },
26
+ }
27
+ );
28
+
29
+ const { error } = await supabase.auth.exchangeCodeForSession(code);
30
+ if (!error) {
31
+ return NextResponse.redirect(`${origin}${next}`);
32
+ }
33
+ }
34
+
35
+ return NextResponse.redirect(`${origin}/?error=auth`);
36
+ }
frontend/src/components/auth-provider.tsx CHANGED
@@ -72,7 +72,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
72
  const { error } = await supabase.auth.signInWithOAuth({
73
  provider: "google",
74
  options: {
75
- redirectTo: `${window.location.origin}/dashboard`,
76
  },
77
  });
78
  if (error) throw error;
 
72
  const { error } = await supabase.auth.signInWithOAuth({
73
  provider: "google",
74
  options: {
75
+ redirectTo: `${window.location.origin}/auth/callback?next=/dashboard`,
76
  },
77
  });
78
  if (error) throw error;
frontend/src/middleware.ts CHANGED
@@ -47,9 +47,9 @@ export async function middleware(request: NextRequest) {
47
 
48
  // Redirect unauthenticated users away from protected routes
49
  if (isProtectedRoute && !user) {
50
- const loginUrl = new URL("/login", request.url);
51
- loginUrl.searchParams.set("redirectTo", pathname);
52
- return NextResponse.redirect(loginUrl);
53
  }
54
 
55
  // Redirect authenticated users away from auth pages
 
47
 
48
  // Redirect unauthenticated users away from protected routes
49
  if (isProtectedRoute && !user) {
50
+ const homeUrl = new URL("/", request.url);
51
+ homeUrl.searchParams.set("redirectTo", pathname);
52
+ return NextResponse.redirect(homeUrl);
53
  }
54
 
55
  // Redirect authenticated users away from auth pages