Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
140 lines (129 loc) · 4.32 KB
/
Copy pathauth.ts
File metadata and controls
140 lines (129 loc) · 4.32 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import NextAuth, { type DefaultSession } from 'next-auth'
import Google from 'next-auth/providers/google'
import config from '@/lib/config'
import { NextResponse } from 'next/server'
import { User, UserRole } from '@/lib/types'
// import { MongoDBAdapter } from '@auth/mongodb-adapter'
// import clientPromise from '@/lib/mongodb'
// import { createUser } from '@/app/services/user-services'
declare module 'next-auth' {
interface Session {
user: User & DefaultSession['user']
}
}
if (!process.env.GOOGLE_CLIENT_ID) {
throw new Error('Invalid/Missing environment variable: "GOOGLE_CLIENT_ID"')
}
if (!process.env.GOOGLE_CLIENT_SECRET) {
throw new Error('Invalid/Missing environment variable: "GOOGLE_CLIENT_SECRET"')
}
export const {
handlers: { GET, POST },
auth,
} = NextAuth({
providers: [
// GitHub,
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
})
],
// adapter: MongoDBAdapter(clientPromise),
session: {
strategy: "jwt",
},
callbacks: {
signIn({ account, profile }) {
if (account?.provider === "google") {
const email = profile?.email ?? ''
const allowedEmailDomains = config?.allowedEmailDomains ?? []
const allowedEmailAddresses = config?.allowedEmailAddresses ?? []
if (!allowedEmailAddresses.length && !allowedEmailDomains.length) {
// If no allowed email addresses or domains are specified, allow all
return true
} else if (allowedEmailAddresses.length && allowedEmailAddresses.includes(email)) {
// If allowed email addresses are specified, check if the user's email is in the list
return true
} else if (allowedEmailDomains.length && allowedEmailDomains.some(
(domain) => email.endsWith(domain)
)) {
// If allowed email domains are specified, check if the user's email domain is in the list
return true
} else {
// Otherwise, deny access
return false
}
} else {
// If the user is not signing in with Google, don't allow access
return false
}
// TODO For marketplace, re-enable closed beta email addresses check
// const docRef = firestore.collection('closed_beta_email_addresses').doc(email.toLowerCase())
// const docSnap = await docRef.get()
// if (docSnap.exists()) {
// return true
// } else {
// return false
// }
},
session: ({ session, token }) => {
return {
...session,
user: {
...session.user,
id: token.id,
role: token.role,
email: token.email,
},
}
},
jwt({ token, profile }) {
if (profile) {
// token.id = profile.id // GitHub
token.id = String(`uid-${profile.sub}`) // Google
token.image = profile.avatar_url || profile.picture
// Set role based on email address
const adminEmailAddresses = config?.adminEmailAddresses ?? []
const adminEmailDomains = config?.adminEmailDomains ?? []
const email = profile.email ?? ''
token.email = email
if (adminEmailAddresses.length && adminEmailAddresses.includes(email)) {
token.role = UserRole.admin
} else if (adminEmailDomains.length && adminEmailDomains.some(
(domain: string) => email.endsWith(domain)
)) {
token.role = UserRole.admin
} else {
token.role = UserRole.user
}
}
return token
},
authorized({ request, auth }) {
const { pathname } = request.nextUrl
const adminPaths = ['/admin']
const publicPaths = config?.publicPaths ?? []
const matchesAdminPath = adminPaths.some((path) =>
pathname.startsWith(path)
)
const matchesPublicPath = publicPaths.some((path) =>
pathname.startsWith(path)
)
if (matchesPublicPath) {
return true
}
if (matchesAdminPath) {
if (!auth?.user) {
return NextResponse.redirect('/sign-in')
}
if (auth?.user?.role !== 'admin') {
return new NextResponse(null, { status: 403 })
}
}
return !!auth?.user
}
},
pages: {
signIn: '/sign-in' // overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
}
})