-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path[provider].get.ts
42 lines (35 loc) · 1.16 KB
/
[provider].get.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
import { joinURL, withQuery } from 'ufo'
import { randomUUID } from 'uncrypto'
export default defineEventHandler(async event => {
const provider = getRouterParam(event, 'provider')
const { refresh } = getQuery(event)
const config = useRuntimeConfig(event)
const session = await getUserSession(event)
if (provider === 'github' && (refresh || !session.data.githubId)) {
const state = randomUUID()
setCookie(event, 'state', state)
return await sendRedirect(
event,
withQuery('https://github.com/login/oauth/authorize', {
client_id: config.github.clientId,
redirect_uri: joinURL(config.url, 'oauth/github'),
state,
})
)
}
if (provider === 'discord' && (refresh || !session.data.discordId)) {
const state = randomUUID()
setCookie(event, 'state', state)
return await sendRedirect(
event,
withQuery('https://discord.com/api/oauth2/authorize', {
client_id: config.discord.clientId,
redirect_uri: joinURL(config.url, 'oauth/discord'),
response_type: 'code',
scope: 'identify guilds.join',
state,
})
)
}
await sendRedirect(event, '/')
})