diff --git a/lib/supabase/dal.ts b/lib/supabase/dal.ts index ea25e45..9d20c2a 100644 --- a/lib/supabase/dal.ts +++ b/lib/supabase/dal.ts @@ -28,10 +28,10 @@ export const getProfile = cache(async () => { export const verifySession = cache(async () => { const user = await getSession() - if (!user) redirect("/login") + if (!user) redirect("/") const profile = await getProfile(); - if(!profile) redirect("/login") + if(!profile) redirect("/") return { userId: user.id, diff --git a/lib/supabase/migrations/20260927100100_create_profile_on_signup_trigger.sql b/lib/supabase/migrations/20260927100100_create_profile_on_signup_trigger.sql index 8147241..2e1b781 100644 --- a/lib/supabase/migrations/20260927100100_create_profile_on_signup_trigger.sql +++ b/lib/supabase/migrations/20260927100100_create_profile_on_signup_trigger.sql @@ -10,7 +10,7 @@ begin new.id, new.email, coalesce(new.raw_user_meta_data->>'name', ''), - coalesce((new.raw_user_meta_data->>'role')::public.user_role, 'client') + 'client' ); return new; end; diff --git a/modules/loginPage/actions/auth.ts b/modules/loginPage/actions/auth.ts index 3db8470..2229bf0 100644 --- a/modules/loginPage/actions/auth.ts +++ b/modules/loginPage/actions/auth.ts @@ -8,7 +8,7 @@ export type AuthState = { error: string } | undefined; const LoginSchema = z.object({ email: z.email({ error: "Bitte eine gültige Email eingeben." }), - password: z.string().min(8, { error: "Passwort ist erforderlich." }), + password: z.string().min(1, { error: "Passwort ist erforderlich." }), }); export async function login( @@ -22,13 +22,15 @@ export async function login( }); if (!validatedFields.success) { - return { error: "Bitte Email und Passwort ausfüllen." } + const { fieldErrors } = z.flattenError(validatedFields.error); + const message = fieldErrors.email?.[0] ?? fieldErrors.password?.[0] ?? "Bitte Email und Passwort ausfüllen."; + return { error: message } } const { email, password } = validatedFields.data; const supabase = await createClient(); - const { error } = await supabase.auth.signInWithPassword({ + const { data: signInData, error } = await supabase.auth.signInWithPassword({ email, password, }); @@ -37,14 +39,17 @@ export async function login( return { error: "Email oder Passwort ist falsch" } } - const { data: { user }} = await supabase.auth.getUser() - const { data: profile } = await supabase + const { data: profile, error: profileError } = await supabase .from("profiles") .select("role") - .eq("id", user!.id) + .eq("id", signInData.user.id) .single() - redirect(profile?.role === "admin" ? "/admin/dashboard" : "/client/dashboard") + if (profileError || !profile) { + return { error: "Profil konnte nicht geladen werden. Bitte kontaktiere den Support." } + } + + redirect(profile.role === "admin" ? "/admin/dashboard" : "/client/dashboard") } export async function logout() { diff --git a/modules/loginPage/ui/views/login-view.tsx b/modules/loginPage/ui/views/login-view.tsx index 69b7c3a..09d77af 100644 --- a/modules/loginPage/ui/views/login-view.tsx +++ b/modules/loginPage/ui/views/login-view.tsx @@ -25,7 +25,7 @@ const LoginPageView = () => {
- Username/Email * + Email * redirectResponse.cookies.set(cookie)) + return redirectResponse } return response