From bc65ca57c2662ae5d34bdec0a9ffd92f16e8c32a Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 5 Feb 2026 17:27:14 +0100 Subject: [PATCH] Supabase proper redirect --- src/app/api/auth/callback/route.ts | 22 +++++++++++++++++++++ src/components/auth/ContinueWith.tsx | 2 +- src/lib/{supabase.ts => supabase/server.ts} | 22 +++++++++++++++++++++ src/modules/account/actions.ts | 7 +++++-- src/modules/account/core.ts | 16 +++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/app/api/auth/callback/route.ts rename src/lib/{supabase.ts => supabase/server.ts} (55%) diff --git a/src/app/api/auth/callback/route.ts b/src/app/api/auth/callback/route.ts new file mode 100644 index 0000000..4ecd122 --- /dev/null +++ b/src/app/api/auth/callback/route.ts @@ -0,0 +1,22 @@ +// app/auth/callback/route.ts +import { NextRequest, NextResponse } from "next/server"; +import { createClient } from "@/lib/supabase/server"; + +export async function GET(req: NextRequest) { + const code = req.nextUrl.searchParams.get("code"); + + if (!code) { + return NextResponse.json({ error: "code param required" }, { status: 400 }); + } + + const supabase = await createClient(); + + const { error } = await supabase.auth.exchangeCodeForSession(code); + + if (error) { + console.error(error); + return NextResponse.json({ error: error.message }, { status: 500 }); + } + + return NextResponse.redirect(new URL("/runs", req.url)); +} diff --git a/src/components/auth/ContinueWith.tsx b/src/components/auth/ContinueWith.tsx index d3c85d1..b5c2cef 100644 --- a/src/components/auth/ContinueWith.tsx +++ b/src/components/auth/ContinueWith.tsx @@ -6,7 +6,7 @@ import { signInWithGitHub } from "@/modules/account/actions"; export default function ContinueWith() { const continueWithGithub = () => { - signInWithGitHub(); + signInWithGitHub(window.location.origin); }; const continueWithGoogle = () => {}; diff --git a/src/lib/supabase.ts b/src/lib/supabase/server.ts similarity index 55% rename from src/lib/supabase.ts rename to src/lib/supabase/server.ts index ae00085..c9431c0 100644 --- a/src/lib/supabase.ts +++ b/src/lib/supabase/server.ts @@ -24,3 +24,25 @@ export async function createClient() { }, ); } + +export async function createClientApi() { + const cookieStore = await cookies(); + + return createServerClient( + process.env.SUPABASE_URL!, + process.env.SUPABASE_SECRET_KEY!, + { + cookies: { + get(name: string) { + return cookieStore.get(name)?.value; + }, + set(name: string, value: string, options: CookieOptions) { + cookieStore.set(name, value, options); + }, + remove(name: string, options: CookieOptions) { + cookieStore.set(name, "", options); + }, + }, + }, + ); +} diff --git a/src/modules/account/actions.ts b/src/modules/account/actions.ts index a2e7820..4df0207 100644 --- a/src/modules/account/actions.ts +++ b/src/modules/account/actions.ts @@ -1,14 +1,17 @@ "use server"; import * as Core from "./core"; -import { createClient } from "@/lib/supabase"; +import { createClient } from "@/lib/supabase/server"; import { redirect } from "next/navigation"; -export async function signInWithGitHub() { +export async function signInWithGitHub(origin: string) { const supabase = await createClient(); const { data, error } = await supabase.auth.signInWithOAuth({ provider: "github", + options: { + redirectTo: `${origin}/api/auth/callback`, + }, }); if (error) { diff --git a/src/modules/account/core.ts b/src/modules/account/core.ts index 7edcfc7..38c4a83 100644 --- a/src/modules/account/core.ts +++ b/src/modules/account/core.ts @@ -66,3 +66,19 @@ export async function getSession( return [user, profile]; } + +export async function signInWithGitHub(supabase: SupabaseClient) { + const { data, error } = await supabase.auth.signInWithOAuth({ + provider: "github", + options: { + redirectTo: `/auth/callback`, + }, + }); + + if (error) { + console.error(error); + throw error; + } + + return data.url; +}