-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
61 lines (52 loc) · 1.54 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
import { db } from "@/lib/db";
import { compareSync } from "bcrypt-ts";
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import email from "next-auth/providers/email";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
providers: [
Credentials({
credentials: {
email: {
},
password: {
}
},
async authorize(credentials, req) {
const password = credentials.password as string
const email = credentials.email as string
if (!email || !password) {
return null;
}
// await getUserByEmail(email);
const user = await db.user.findUnique({
where: {
email: email
}
})
if (!user) {
return null;
}
const matches = compareSync(password, user.password ?? "")
if (matches) {
return { id: user.id, email: user.email, name: user.name, role: user.role }
}
return null;
}
}),
], callbacks: {
async jwt({ token, user }) {
user && (token.user = user)
return token
},
async session({ session, token }) {
session = token.user as any
return session
}
}
});