mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-18 04:36:21 +02:00
feat: implement user authentication and authorization with NextAuth, including user schema, password hashing, and role-based access control.
This commit is contained in:
1 parent
c842c2447a
commit
359d196c4d
18 files changed
+652
-32
No files matched your search
+60
-2
@@ -1,6 +1,64 @@
|
||||
import NextAuth from "next-auth";
|
||||
import GitHub from "next-auth/providers/github"; // Example provider
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { db } from "@/db";
|
||||
import { users } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { verifyPassword } from "@/features/auth/logic/auth-logic";
|
||||
import { ensureAdminExists } from "@/features/auth/logic/seed-admin";
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
providers: [GitHub],
|
||||
providers: [
|
||||
Credentials({
|
||||
name: "Credentials",
|
||||
credentials: {
|
||||
username: { label: "Utilisateur", type: "text" },
|
||||
password: { label: "Mot de passe", type: "password" }
|
||||
},
|
||||
async authorize(credentials) {
|
||||
// S'assure que le compte admin par défaut existe
|
||||
await ensureAdminExists();
|
||||
|
||||
if (!credentials?.username || !credentials?.password) return null;
|
||||
|
||||
try {
|
||||
const [user] = await db.select()
|
||||
.from(users)
|
||||
.where(eq(users.username, credentials.username as string));
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
const isValid = verifyPassword(credentials.password as string, user.passwordHash);
|
||||
if (!isValid) return null;
|
||||
|
||||
return {
|
||||
id: user.id.toString(),
|
||||
name: user.username,
|
||||
role: user.role,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("Auth Error:", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.role = (user as any).role;
|
||||
token.id = user.id;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
if (session.user) {
|
||||
(session.user as any).role = token.role as string;
|
||||
(session.user as any).id = token.id as string;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user