-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
146 lines (139 loc) · 3.59 KB
/
auth.ts
File metadata and controls
146 lines (139 loc) · 3.59 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import CredentialsProvider from "next-auth/providers/credentials";
import NextAuth, { Session, User } from "next-auth";
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
import * as z from "zod";
declare module "next-auth" {
interface User {
role: string,
id: number|string,
}
}
export const { auth, handlers, signIn, signOut } = NextAuth({
session: { strategy: "jwt" },
providers: [
CredentialsProvider({
name: "credentials",
credentials: {
login: { label: "Login", type: "login" },
name: { label: "Name", type: "name" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
let login: string, name: string|undefined, password: string;
try {
({login, password, name} = z.object(
{
login: z.union([z.email(), z.e164()]),
name: z.string().min(6).max(128).optional(),
password: z.string().min(6).max(128),
}
).parse(credentials));
} catch(error: any) {
throw new Error(error.issues);
}
let patient = await prisma.patient.findFirst({
where: {
OR: [
{phone: login},
{whatsapp: login},
{email: login},
]
}
});
if (patient) {
if (await bcrypt.compare(
password,
patient.password || ''
)) {
return {
name: patient.name,
id: patient.id,
role: 'patient'}
} else {
throw new Error("Invalid credentials");
}
}
const doctor = await prisma.doctor.findFirst({
where: {
OR: [
{phone: login},
{whatsapp: login},
{email: login},
]
}
})
if (doctor) {
if (await bcrypt.compare(
password,
doctor.password || ''
)) {
return {
name: doctor.name,
id: doctor.id,
role: 'doctor'
}
} else {
throw new Error("Invalid credentials");
}
}
const user = await prisma.user.findFirst({
where: {
OR: [
{email: login},
]
}
})
if (user) {
if (await bcrypt.compare(
password,
user.password || ''
)) {
return {
name: user.name,
id: user.id,
role: user.role,
}
} else {
throw new Error("Invalid credentials");
}
}
if (name) {
const _patient = await prisma.patient.create({
data: {
name,
whatsapp: login,
password: await bcrypt.hash(password, 10),
},
});
return {
name: _patient.name,
id: _patient.id,
role: 'patient',
};
}
return null;
},
}),
],
pages: {
signIn: "/login",
},
callbacks: {
async jwt({ token, user }) {
console.log('jwt', { token, user })
return {
...token,
id: token.id ?? user?.id,
role: token.role ?? user?.role,
};
},
async session({ session, token }) {
console.log({session, token })
if (token)
return { ...session, user: { ...session.user, id: Number(token.id), role: String(token?.role)}};
else
return session;
},
},
});