-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.ts
61 lines (54 loc) · 1.4 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 { PrismaAdapter } from '@auth/prisma-adapter'
import bcrypt from 'bcryptjs'
import NextAuth from 'next-auth'
import Credentials from 'next-auth/providers/credentials'
import GitHub from 'next-auth/providers/github'
import Google from 'next-auth/providers/google'
import db from '@/lib/db'
import { SignInSchema } from '@/schemas'
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(db),
session: {
strategy: 'jwt',
},
providers: [
Credentials({
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
const { email, password } = await SignInSchema.parseAsync(credentials)
const user = await db.user.findUnique({
where: {
email,
},
})
if (!user) {
throw new Error('No user found')
}
const isValid = bcrypt.compareSync(password, user.password!)
if (!isValid) {
throw new Error('Invalid password')
}
return {
id: user.id,
name: user.name,
email: user.email,
image: user.image,
}
},
}),
GitHub,
Google,
],
callbacks: {
session({ session, token }) {
if (token.sub && session.user) {
session.user.id = token.sub
}
return session
},
},
debug: process.env.NODE_ENV === 'development',
})