aboutsummaryrefslogtreecommitdiff
path: root/src/routes/login.ts
blob: 1cd9b23c937ee0fd14d88a39ea5299f62565fbf7 (plain)
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
import type { RequestHandler } from '@sveltejs/kit'
import env from '$lib/env'


export const get: RequestHandler = async ({ request, platform }) => {
	const host = request.headers.get('host')

	const clientId = env(platform).DISCORD_CLIENT_ID

	if (!clientId)
		return {
			status: 500,
			headers: {
				'content-type': 'text/plain',
			},
			body: 'DISCORD_CLIENT_ID is not set as an environment variable. Please contact the owner of the website if this was expected to work.',
		}

	if (!host)
		return {
			status: 400,
			headers: {
				'content-type': 'text/plain',
			},
			body: 'Host header is required.',
		}

	const protocol = request.url.startsWith('https') ? 'https' : 'http'

	return {
		status: 303,
		headers: {
			location: `https://discord.com/oauth2/authorize?client_id=${clientId}&redirect_uri=${protocol}://${host}%2Floggedin&response_type=code&scope=identify`
		}
	}
}