mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 19:51:17 +00:00
see comments
This commit is contained in:
parent
478adcabca
commit
bc71928e1c
5 changed files with 19 additions and 12 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
4
proxy.ts
4
proxy.ts
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue