aboutsummaryrefslogtreecommitdiff
path: root/src/routes/logout/+server.ts
blob: 2e0f40d627524cd511bf37ee8c8f0e6b85c2d948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { fetchApi } from '$lib/api'
import { redirect, type RequestHandler } from '@sveltejs/kit'

export const GET = (async ({ url, cookies, locals }) => {
	// if the sid is wrong, nothing to do
	if (url.searchParams.has('sid') && url.searchParams.get('sid') === locals.sid) {
		await fetchApi(`accounts/session`, fetch, {
			method: 'DELETE',
			headers: {
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({
				uuid: locals.sid
			}),
		}).then(res => {
			if (res.status !== 200)
				throw new Error(res.statusText)
		})
	}

	cookies.delete('sid')
	throw redirect(303, '/')
}) as RequestHandler