-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.ts
64 lines (61 loc) · 1.48 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
import NextAuth from "next-auth"
import Credentials from "next-auth/providers/credentials"
import { signInSchema } from "./lib/zod"
import bcrypt from "bcryptjs"
import { prisma } from "./app/db"
import { JWT } from "next-auth/jwt"
declare module "next-auth/jwt" {
interface JWT {
id?: string
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
authorize: async (credentials) => {
if (!credentials) {
return null
}
// fetch credentials
const { email, password } = await signInSchema.parseAsync(credentials)
// search for the user
const userExist = await prisma.user.findUnique({
where: {
email
}
})
// if user not found
if (!userExist || !userExist.password) {
return null
}
// compare the hashed pass
const hashedPasswordValidate = await bcrypt.compare(password, userExist?.password);
// if password not validates
if (!hashedPasswordValidate) {
return null
}
return userExist
},
}),
],
pages: {
signIn: '/auth/signin', // route to sign in page
},
session: {
strategy: "jwt"
},
callbacks: {
jwt({ token, user }) {
if (user) {
token.id = user.id
}
return token
},
session({ session, token }) {
if (token.id) {
session.user.id = token.id
}
return session
},
},
})