Fix: pasar el signin por endpoint propio para completar PKCE
El flow OAuth de Supabase (@supabase/ssr) es PKCE por default: requiere guardar un code_verifier en cookies del cliente ANTES de mandar al user a Google. El login.astro anterior linkeaba directo a supabase.buglabs.dev/auth/v1/authorize saltándose ese paso, así que el exchangeCodeForSession del callback fallaba sin cookies válidas. Ahora el botón "Continuar con Google" apunta a /api/auth/signin, que usa supabase.auth.signInWithOAuth() — este método sí setea el code_verifier en las cookies del dominio de la app, y después el callback lo recupera para completar el intercambio. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Vendored
+2
@@ -30,3 +30,5 @@ interface ImportMetaEnv {
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
|
||||
declare const process: { env: Record<string, string | undefined> };
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { serverClient } from '@/lib/supabase';
|
||||
|
||||
export const GET: APIRoute = async ({ cookies, url, redirect }) => {
|
||||
const appUrl = process.env.PUBLIC_APP_URL ?? import.meta.env.PUBLIC_APP_URL ?? url.origin;
|
||||
const supabase = serverClient(cookies);
|
||||
|
||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||
provider: 'google',
|
||||
options: { redirectTo: `${appUrl}/api/auth/callback` },
|
||||
});
|
||||
|
||||
if (error || !data?.url) return redirect('/login?error=oauth');
|
||||
return redirect(data.url);
|
||||
};
|
||||
@@ -9,10 +9,7 @@ const errorMsg = errorParam === 'dominio'
|
||||
? 'No se pudo completar la autenticación con Google. Inténtalo de nuevo.'
|
||||
: null;
|
||||
|
||||
const appUrl = process.env.PUBLIC_APP_URL ?? import.meta.env.PUBLIC_APP_URL ?? url.origin;
|
||||
const supabaseUrl = process.env.PUBLIC_SUPABASE_URL ?? import.meta.env.PUBLIC_SUPABASE_URL;
|
||||
const redirectTo = `${appUrl}/api/auth/callback`;
|
||||
const googleAuthUrl = `${supabaseUrl}/auth/v1/authorize?provider=google&redirect_to=${encodeURIComponent(redirectTo)}`;
|
||||
const googleAuthUrl = '/api/auth/signin';
|
||||
---
|
||||
<Layout title="Iniciar sesión — LabPréstamos UABC">
|
||||
<main id="main" class="min-h-screen grid place-items-center p-4">
|
||||
|
||||
Reference in New Issue
Block a user