mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-22 22:48:08 +02:00
feat: implement user authentication and route protection using NextAuth middleware and server actions.
This commit is contained in:
1 parent
a03fd64b5d
commit
44d7f95d4c
2 files changed
+3
-3
No files matched your search
@@ -11,7 +11,7 @@ export async function loginAction(prevState: string | undefined, formData: FormD
|
|||||||
await signIn("credentials", {
|
await signIn("credentials", {
|
||||||
username: formData.get("username"),
|
username: formData.get("username"),
|
||||||
password: formData.get("password"),
|
password: formData.get("password"),
|
||||||
redirectTo: "/dashboard"
|
redirectTo: "/grid"
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AuthError) {
|
if (error instanceof AuthError) {
|
||||||
|
|||||||
+2
-2
@@ -21,7 +21,7 @@ export default auth((req) => {
|
|||||||
|
|
||||||
// 3. Rediriger vers le dashboard si déjà connecté et sur /login
|
// 3. Rediriger vers le dashboard si déjà connecté et sur /login
|
||||||
if (isLoggedIn && isPublicRoute) {
|
if (isLoggedIn && isPublicRoute) {
|
||||||
return NextResponse.redirect(new URL("/dashboard", nextUrl));
|
return NextResponse.redirect(new URL("/grid", nextUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Protection par rôle (Admin seulement pour les paramètres)
|
// 4. Protection par rôle (Admin seulement pour les paramètres)
|
||||||
@@ -29,7 +29,7 @@ export default auth((req) => {
|
|||||||
const userRole = (req.auth?.user as any)?.role;
|
const userRole = (req.auth?.user as any)?.role;
|
||||||
|
|
||||||
if (isAdminRoute && userRole !== "admin") {
|
if (isAdminRoute && userRole !== "admin") {
|
||||||
return NextResponse.redirect(new URL("/dashboard", nextUrl));
|
return NextResponse.redirect(new URL("/grid", nextUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
|
|||||||
Reference in new issue
Block a user