-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.mjs
266 lines (252 loc) · 6.54 KB
/
auth.config.mjs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { DAY } from "@/lib/interfaces";
import Discord from "@auth/core/providers/discord";
import { EdgeDBAdapter } from "@auth/edgedb-adapter"
import GitHub from "@auth/core/providers/github";
import Resend from "@auth/core/providers/resend";
import { client } from "./client"
import { defineConfig } from "auth-astro";
import { sendVerificationRequest } from "./src/lib/emailLogin"
let extraProviders = []
if (import.meta.env.AUTH_DISCORD_ID) {
extraProviders.push(Discord({
clientId: import.meta.env.AUTH_DISCORD_ID,
clientSecret: import.meta.env.AUTH_DISCORD_SECRET,
}))
}
if (import.meta.env.AUTH_RESEND_KEY) {
extraProviders.push(Resend({
apiKey: import.meta.env.AUTH_RESEND_KEY,
from: "hello@l.dankdeck.xyz",
sendVerificationRequest: sendVerificationRequest
}))
}
const userProps = `
id,
email,
emailVerified,
name,
picture,
theme,
nsfw,
bio,
githubName,
discordName,
image
`
export default defineConfig({
baseUrl: import.meta.env.AUTH_URL,
providers: [
GitHub({
clientId: import.meta.env.GITHUB_CLIENT_ID,
clientSecret: import.meta.env.GITHUB_CLIENT_SECRET,
redirectProxyUrl: import.meta.env.AUTH_URL ? `${import.meta.env.AUTH_URL}api/auth/callback/github` : undefined,
}),
...extraProviders
],
session: {
maxAge: DAY * 90,
strategy: "database",
updateAge: DAY * 14,
},
adapter: {
...EdgeDBAdapter(client),
async createUser({ email, emailVerified, name, image, ...rest }) {
return await client.queryRequiredSingle(
`
with
image := <optional str>$image,
name := <optional str>$name,
emailVerified := <optional str>$emailVerified
select (
insert User {
email:= <str>$email,
emailVerified:= <datetime>emailVerified,
name:= name,
image:= image,
}
) {
${userProps}
}
`,
{
email,
emailVerified: emailVerified && new Date(emailVerified).toISOString(),
name,
image,
}
)
},
async createUser({ email, emailVerified, name, image, ...props }) {
return await client.queryRequiredSingle(
`
with
image := <optional str>$image,
name := <optional str>$name,
emailVerified := <optional str>$emailVerified
select (
insert User {
email:= <str>$email,
emailVerified:= <datetime>emailVerified,
name:= name,
image:= image,
}
) {
${userProps}
}
`,
{
email,
emailVerified: emailVerified && new Date(emailVerified).toISOString(),
name,
image,
}
)
},
async getUser(id) {
return await client.querySingle(
`
select User {
${userProps}
} filter .id = <uuid>$id;
`,
{ id }
)
},
async getUserByEmail(email) {
return await client.querySingle(
`
select User {
${userProps}
} filter .email = <str>$email;
`,
{ email }
)
},
async getUserByAccount({ providerAccountId, provider, ...props }) {
return await client.querySingle(
`
with account := (
select Account
filter .providerAccountId = <str>$providerAccountId
and .provider = <str>$provider
)
select account.user {
${userProps}
}
`,
{ providerAccountId, provider }
)
},
async updateUser({ email, emailVerified, id, image, name, ...props }) {
return await client.queryRequiredSingle(
`
with
email := <optional str>$email,
emailVerified := <optional str>$emailVerified,
image := <optional str>$image,
name := <optional str>$name
select (
update User
filter .id = <uuid>$id
set {
email := email ?? .email,
emailVerified := <datetime>emailVerified ?? .emailVerified,
image := image ?? .image,
name := name ?? .name,
}
) {
${userProps}
}
`,
{
email,
emailVerified: emailVerified && new Date(emailVerified).toISOString(),
id,
image,
name,
}
)
},
async getSessionAndUser(sessionToken) {
const sessionAndUser = await client.querySingle(
`
select Session {
userId,
id,
expires,
sessionToken,
user: {
${userProps}
}
} filter .sessionToken = <str>$sessionToken;
`,
{ sessionToken }
)
if (!sessionAndUser) {
return null
}
const { user, ...session } = sessionAndUser
if (!user || !session) {
return null
}
return {
user,
session,
}
},
},
pages: {
signIn: "/login",
newUser: "/cards?onboard=1"
},
callbacks: {
session: async ({ session, user, ...props }) => {
session.user.id = user.id;
session.user.picture = user.picture
return session;
},
signIn: async ({ user, account, profile, ...props }) => {
// little trick :)
setTimeout(async () => {
try {
if (account.provider == "github" && user.githubName != profile.login) {
await client.querySingle(`
update User
filter
.id = <uuid>$userId
set {
githubName := <str>$login
}
`, {
userId: user.id,
login: profile.login
})
}
else if (account.provider == "discord" && user.discordName != profile.username) {
await client.querySingle(`
update User
filter
.id = <uuid>$userId
set {
discordName := <str>$login
}
`, {
userId: user.id,
login: profile.username
})
}
} catch (e) {
// non essential no need to handle errors lol
console.log(e)
}
}, 0)
return true
},
redirect: async ({ url, baseUrl }) => {
if (url.startsWith("/")) return `${baseUrl}${url}`
if (new URL(url).origin === baseUrl) return url
if (url.includes("app.github.dev")) return url;
return baseUrl
},
}
});