see comments

This commit is contained in:
Rajpreet0 2026-09-27 01:48:15 +02:00
parent 478adcabca
commit bc71928e1c
5 changed files with 19 additions and 12 deletions

View file

@ -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,

View file

@ -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;

View file

@ -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() {

View file

@ -25,7 +25,7 @@ const LoginPageView = () => {
<form action={formAction} className="w-full max-w-sm ">
<FieldGroup>
<Field>
<FieldLabel htmlFor="form-email"><User className="w-4 h-4"/> Username/Email <span className="text-destructive">*</span></FieldLabel>
<FieldLabel htmlFor="form-email"><User className="w-4 h-4"/> Email <span className="text-destructive">*</span></FieldLabel>
<Input
id="form-email"
name="email"

View file

@ -12,7 +12,9 @@ export default async function proxy(request: NextRequest) {
const isPublicRoute = publicRoutes.includes(request.nextUrl.pathname)
if (!user && !isPublicRoute) {
return NextResponse.redirect(new URL("/", request.url))
const redirectResponse = NextResponse.redirect(new URL("/", request.url))
response.cookies.getAll().forEach((cookie) => redirectResponse.cookies.set(cookie))
return redirectResponse
}
return response