feat: handle redirects for admin in /order page and implement next query param on login
All checks were successful
Staging Build / build (push) Successful in 2m19s
All checks were successful
Staging Build / build (push) Successful in 2m19s
This commit is contained in:
@@ -31,19 +31,36 @@ async function OrderDataWrapper() {
|
|||||||
const { data: { user } } = await supabase.auth.getUser()
|
const { data: { user } } = await supabase.auth.getUser()
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
redirect('/auth/login')
|
redirect('/auth/login?next=/order')
|
||||||
}
|
}
|
||||||
|
|
||||||
const products = await getProducts()
|
// Check user role
|
||||||
const categories = await getCategories()
|
const { data: userData } = await supabase
|
||||||
const endCustomers = await getEndCustomers()
|
.from('users')
|
||||||
|
.select('role')
|
||||||
// Fetch profile if exists
|
|
||||||
const { data: profile } = await supabase
|
|
||||||
.from('profiles')
|
|
||||||
.select('*')
|
|
||||||
.eq('id', user.id)
|
.eq('id', user.id)
|
||||||
.single()
|
.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 <OrderWizard products={products} categories={categories} initialProfile={profile} initialEndCustomers={endCustomers} />
|
return <OrderWizard products={products} categories={categories} initialProfile={profile} initialEndCustomers={endCustomers} />
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,14 @@ export function LoginForm({
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
|
const nextParam = typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('next') : null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await signIn(email, password);
|
const res = await signIn(email, password);
|
||||||
if (res.role === "admin") {
|
if (res.role === "admin") {
|
||||||
router.push("/admin/einstellungen");
|
router.push("/admin/einstellungen");
|
||||||
} else {
|
} else {
|
||||||
router.push("/my-customers");
|
router.push(nextParam || "/my-customers");
|
||||||
}
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
setError(error instanceof Error ? error.message : "An error occurred");
|
setError(error instanceof Error ? error.message : "An error occurred");
|
||||||
|
|||||||
Reference in New Issue
Block a user