From 7be84681f00058f61560b160f9ff29fcca4bc05c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Feb 2026 17:03:51 +0000 Subject: [PATCH] fix: login redirect with Suspense boundary + read redirect query param - Wrap useSearchParams() in Suspense boundary (Next.js requirement) - Read ?redirect= query param for post-login redirect to /admin - Auto-detect admin users and redirect to /admin - Fallback to /dashboard for regular users https://claude.ai/code/session_01H2aRGDaKgarPvhay2HxN6Y --- app/login/page.tsx | 142 +++++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 58 deletions(-) diff --git a/app/login/page.tsx b/app/login/page.tsx index 67c0d06..391a73e 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,14 +1,16 @@ "use client"; -import { useState } from "react"; +import { Suspense, useState } from "react"; import Link from "next/link"; -import { useRouter } from "next/navigation"; +import { useRouter, useSearchParams } from "next/navigation"; import { createClient } from "@/lib/supabase/client"; import Button from "@/components/ui/Button"; import Input from "@/components/ui/Input"; -export default function LoginPage() { +function LoginForm() { const router = useRouter(); + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirect"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); @@ -35,18 +37,31 @@ export default function LoginPage() { return; } - // Vérifier si l'utilisateur est admin pour la redirection - if (authData.user) { - const { data: profile } = await supabase - .from("profiles") - .select("is_admin") - .eq("id", authData.user.id) - .single(); + // Si un redirect est spécifié dans l'URL, l'utiliser directement + if (redirectTo) { + router.push(redirectTo); + router.refresh(); + return; + } - if (profile && (profile as { is_admin?: boolean }).is_admin) { - router.push("/admin"); - router.refresh(); - return; + // Sinon, vérifier si l'utilisateur est admin + if (authData.user) { + try { + const { data: profile } = await supabase + .from("profiles") + .select("is_admin, subscription_status") + .eq("id", authData.user.id) + .single(); + + const p = profile as { is_admin?: boolean; subscription_status?: string } | null; + + if (p?.is_admin) { + router.push("/admin"); + router.refresh(); + return; + } + } catch { + // RLS peut bloquer, on continue vers dashboard } } @@ -59,6 +74,55 @@ export default function LoginPage() { } }; + return ( +
+
+ setEmail(e.target.value)} + required + /> + setPassword(e.target.value)} + required + /> + + {error && ( +
+

{error}

+
+ )} + + +
+ +
+

+ Pas encore de compte ?{" "} + + Candidater + +

+
+
+ ); +} + +export default function LoginPage() { return (
@@ -80,51 +144,13 @@ export default function LoginPage() {

- {/* Form */} -
-
- setEmail(e.target.value)} - required - /> - setPassword(e.target.value)} - required - /> - - {error && ( -
-

{error}

-
- )} - - -
- -
-

- Pas encore de compte ?{" "} - - Candidater - -

+ +
-
+ }> + +
);