Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
82 lines (76 loc) · 2.39 KB
/
Copy pathauth.ts
File metadata and controls
82 lines (76 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { prisma } from "@/app/lib/prisma";
import bcrypt from "bcryptjs";
export const { handlers, signIn, signOut, auth } = NextAuth({
trustHost: true,
providers: [
Credentials({
id: "unified",
name: "Unified",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Mot de passe", type: "password" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) return null;
const emailStr = (credentials.email as string).trim().toLowerCase();
const passStr = credentials.password as string;
// 1. Priorité Administrateur : Vérification dans la table Admin
const admin = await prisma.admin.findUnique({
where: { email: emailStr },
});
if (admin) {
const validAdmin = await bcrypt.compare(passStr, admin.passwordHash);
if (validAdmin) {
return {
id: admin.id,
email: admin.email,
name: admin.name,
role: admin.role || "admin",
};
}
return null; // Si l'email est admin mais mot de passe faux, ne pas chercher dans Customer
}
// 2. Espace Client Acheteur : Vérification dans la table Customer
const customer = await prisma.customer.findUnique({
where: { email: emailStr },
});
if (customer) {
const validCustomer = await bcrypt.compare(passStr, customer.passwordHash);
if (validCustomer) {
return {
id: customer.id,
email: customer.email,
name: customer.name,
role: "customer",
};
}
}
return null;
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.role = (user as { role?: string }).role;
token.id = user.id;
}
return token;
},
async session({ session, token }) {
if (session.user) {
(session.user as { role?: string; id?: string }).role =
token.role as string;
(session.user as { role?: string; id?: string }).id =
token.id as string;
}
return session;
},
},
pages: {
signIn: "/compte/connexion",
},
session: { strategy: "jwt" },
});