-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
120 lines (111 loc) · 3.53 KB
/
Copy pathauth.ts
File metadata and controls
120 lines (111 loc) · 3.53 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
import NextAuth from 'next-auth'
import Google from 'next-auth/providers/google'
import Credentials from 'next-auth/providers/credentials'
import { MongoDBAdapter } from '@auth/mongodb-adapter'
import { isGoogleAuthEnabled, subdomainAuthCookies } from '@/lib/auth/cookies'
import { clientPromise } from '@/lib/mongodb-client'
import { findUserById, serializeProfile, verifyCredentials } from '@/lib/users'
const defaultProfile = {
username: '',
displayName: '',
supportedCountry: '',
favoritePlayer: '',
onboardingComplete: false,
founderWelcomeSent: false,
emailAlerts: true,
alertFavoritePlayer: true,
alertSupportedCountry: true,
}
const sharedCookieDomain = process.env.AUTH_COOKIE_DOMAIN
export const { handlers, auth, signIn, signOut } = NextAuth({
trustHost: true,
adapter: MongoDBAdapter(clientPromise),
session: { strategy: 'jwt' },
pages: {
signIn: '/auth/signin',
},
...(sharedCookieDomain ? { cookies: subdomainAuthCookies(sharedCookieDomain) } : {}),
providers: [
...(isGoogleAuthEnabled()
? [
Google({
clientId: process.env.AUTH_GOOGLE_ID!,
clientSecret: process.env.AUTH_GOOGLE_SECRET!,
allowDangerousEmailAccountLinking: true,
}),
]
: []),
Credentials({
name: 'Email',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
const email = credentials?.email
const password = credentials?.password
if (!email || !password || typeof email !== 'string' || typeof password !== 'string') {
return null
}
const user = await verifyCredentials(email, password)
if (!user) return null
return {
id: String(user._id),
email: user.email,
name: user.name,
image: user.image,
profile: serializeProfile(user),
}
},
}),
],
callbacks: {
async jwt({ token, user, trigger, session }) {
if (user?.id) {
token.id = user.id
token.profile = user.profile ?? defaultProfile
}
if (trigger === 'update' && session?.profile) {
token.profile = session.profile
}
if (typeof token.id === 'string' && (trigger === 'update' || !token.profile)) {
const dbUser = await findUserById(token.id)
if (dbUser) {
token.profile = serializeProfile(dbUser)
token.name = dbUser.name ?? token.name
token.email = dbUser.email ?? token.email
token.picture = dbUser.image ?? token.picture
}
}
return token
},
async session({ session, token }) {
if (session.user && typeof token.id === 'string') {
session.user.id = token.id
session.user.profile = token.profile ?? defaultProfile
}
return session
},
},
events: {
async createUser({ user }) {
if (!user.id || !user.email) return
const { ObjectId } = await import('mongodb')
const { getDb } = await import('@/lib/mongodb-client')
const { sendWelcomeEmail } = await import('@/lib/emails')
const db = await getDb()
await db.collection('users').updateOne(
{ _id: new ObjectId(user.id) },
{
$set: {
profile: defaultProfile,
updatedAt: new Date(),
},
}
)
sendWelcomeEmail(user.email, user.name || 'there').catch((error) => {
console.error('OAuth welcome email failed:', error)
})
},
},
})