-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
76 lines (64 loc) · 1.96 KB
/
auth.ts
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
import { createUser } from "@/lib/actions/users";
import NextAuth, { User } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { z } from "zod";
import { upsertAdmin } from "@/lib/actions/users";
import { authConfig } from "./auth.config";
export const { auth, unstable_update, signIn, signOut, handlers } = NextAuth({
...authConfig,
providers: [
Credentials({
id: "app-login",
name: "app",
credentials: {
name: { label: "pseudo", type: "text", placeholder: "pseudo" },
},
async authorize(credentials): Promise<User | null> {
const parsedCredentials = z
.object({
pseudo: z
.string()
.transform((t) => t?.trim())
.pipe(z.string().min(1)),
})
.safeParse(credentials);
if (parsedCredentials.success) {
const { pseudo } = parsedCredentials.data;
const user = await createUser(pseudo);
if (!user) return null;
return user;
}
console.log("Pseudo schema validation failed.");
return null;
},
}),
Credentials({
id: "admin-login",
name: "admin",
credentials: {
email: { label: "email", type: "email", placeholder: "email" },
password: {
label: "password",
type: "password",
placeholder: "password",
},
},
async authorize(credentials): Promise<User | null> {
const parsedCredentials = z
.object({
email: z.string().email(),
password: z.literal(process.env.ADMIN_PASSWORD),
})
.safeParse(credentials);
if (parsedCredentials.success) {
const { email } = parsedCredentials.data;
const user = await upsertAdmin(email);
if (!user) return null;
return user;
}
console.log("Identifiants incorrects.");
return null;
},
}),
],
});