aboutsummaryrefslogtreecommitdiff
path: root/src/routes/loggedin.ts
blob: 9ae28efe9fa7fb7bf210ec19927ad5c2fa5feb68 (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
37
import { fetchApi } from '$lib/api'
import type { RequestHandler } from '@sveltejs/kit'

export const get: RequestHandler = async ({ url }) => {
	const code = url.searchParams.get('code')
	const redirectUri = `${url.protocol}//${url.host}/loggedin`
	const response = await fetchApi(`accounts/createsession`, fetch, {
		method: 'POST',
		headers: {
			'content-type': 'application/json',
		},
		body: JSON.stringify({
			code,
			redirectUri: redirectUri
		}),
	}).then(res => {
		if (res.status !== 200)
			throw new Error(res.statusText)
		return res.json()
	})

	if (response.ok) {
		return {
			status: 303,
			headers: {
				location: '/verify',
				'Set-Cookie': `sid=${response.session_id}; Max-Age=31536000000; Path=/; HttpOnly`
			}
		}
	}
	return {
		status: 303,
		headers: {
			location: '/login',
		}
	}
}