-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
114 lines (108 loc) · 3.55 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
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
import authConfig from '@/auth.config'
import { getTwoFactorConfirmationByUserId } from '@/lib/actions/two-factor-confirmation'
import { getUserById } from '@/lib/actions/user.action'
import { getAccountByUserId } from '@/lib/data/account'
import prisma from '@/lib/prisma'
import { PrismaAdapter } from '@auth/prisma-adapter'
import { UserRole } from '@prisma/client'
import NextAuth, { type DefaultSession } from 'next-auth'
import { DEFAULT_JWT_EXPIRES_IN } from '@/config'
import { JWT } from 'next-auth/jwt'
export type ExtendedUser = {
id: string
role: UserRole
isTwoFactorEnabled: boolean
isOAuth: boolean
} & DefaultSession['user']
// extend the session to include the user id and role
declare module 'next-auth' {
interface Session {
user: ExtendedUser
}
}
declare module 'next-auth/jwt' {
/** Returned by the `jwt` callback and `auth`, when using JWT sessions */
interface JWT {
role?: UserRole
}
}
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
session: {
strategy: 'jwt',
maxAge: DEFAULT_JWT_EXPIRES_IN * 24 * 60 * 60
},
pages: {
signIn: '/sign-in',
error: '/error'
},
events: {
async linkAccount({ user }) {
await prisma.user.update({
where: {
id: user.id
},
data: {
emailVerified: new Date()
}
})
}
},
callbacks: {
// signIn callback checks if the user exists and if the email is verified
async signIn({ user, account }) {
if (account?.provider !== 'credentials') {
return true
}
if (!user.id) return false
// prevent the user from signing in if the email is not verified
const existingUser = await getUserById(user.id)
if (!existingUser?.emailVerified) return false
// if the user has two factor enabled then check if the two factor confirmation exists
if (existingUser.isTwoFactorEnabled) {
const twoFactorConfirmation = await getTwoFactorConfirmationByUserId(existingUser.id)
if (!twoFactorConfirmation) return false
await prisma.twoFactorConfirmation.delete({
where: {
id: twoFactorConfirmation.id
}
})
}
return true
},
// jwt callback returns the token and pass it to the session callback
async jwt({ token }) {
// console.log('🚀 ~ callbacks jwt ~ { token }:', { token })
// token.sub is the user id
if (!token.sub) return token
// check if the user exists in the database
const existingUser = await getUserById(token.sub)
if (!existingUser) return token
const existingAccount = await getAccountByUserId(existingUser.id)
// if the user exists then add the user info to the token
token.name = existingUser.name
token.email = existingUser.email
token.role = existingUser.role as UserRole
token.isTwoFactorEnabled = existingUser.isTwoFactorEnabled
token.isOAuth = !!existingAccount
return token
},
async session({ token, session }) {
console.log('🚀 ~ callbacks session ~ token:', { token })
if (token.sub && session.user) {
session.user.id = token.sub
}
if (token.role && session.user) {
session.user.role = token.role as UserRole
}
if (session.user) {
session.user.name = token.name
session.user.email = token.email as string
session.user.isTwoFactorEnabled = token.isTwoFactorEnabled as boolean
session.user.isOAuth = token.isOAuth as boolean
}
return session
}
},
...authConfig
})