From a56eb93ffcf1e61b484bb78ab6724473d3a42596 Mon Sep 17 00:00:00 2001 From: DanielS Date: Wed, 24 Jun 2026 00:29:46 +0200 Subject: [PATCH] feat: handle redirects for admin in /order page and implement next query param on login --- shop/app/order/page.tsx | 35 +++++++++++++++++++++++++--------- shop/components/login-form.tsx | 4 +++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/shop/app/order/page.tsx b/shop/app/order/page.tsx index 69d1ef1..0ddbc3b 100644 --- a/shop/app/order/page.tsx +++ b/shop/app/order/page.tsx @@ -31,19 +31,36 @@ async function OrderDataWrapper() { const { data: { user } } = await supabase.auth.getUser() if (!user) { - redirect('/auth/login') + redirect('/auth/login?next=/order') } - const products = await getProducts() - const categories = await getCategories() - const endCustomers = await getEndCustomers() - - // Fetch profile if exists - const { data: profile } = await supabase - .from('profiles') - .select('*') + // Check user role + const { data: userData } = await supabase + .from('users') + .select('role') .eq('id', user.id) .single() + if (userData?.role === 'admin') { + redirect('/admin/einstellungen') + } + + const products = await getProducts().catch(() => []) + const categories = await getCategories().catch(() => []) + const endCustomers = await getEndCustomers().catch(() => []) + + // Fetch profile safely using maybeSingle + let profile = null + try { + const { data } = await supabase + .from('profiles') + .select('*') + .eq('id', user.id) + .maybeSingle() + profile = data + } catch (e) { + console.error('Fehler beim Laden des Profils:', e) + } + return } diff --git a/shop/components/login-form.tsx b/shop/components/login-form.tsx index b13560e..d38b17d 100644 --- a/shop/components/login-form.tsx +++ b/shop/components/login-form.tsx @@ -31,12 +31,14 @@ export function LoginForm({ setIsLoading(true); setError(null); + const nextParam = typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('next') : null; + try { const res = await signIn(email, password); if (res.role === "admin") { router.push("/admin/einstellungen"); } else { - router.push("/my-customers"); + router.push(nextParam || "/my-customers"); } } catch (error: unknown) { setError(error instanceof Error ? error.message : "An error occurred");