aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/login/+page.server.js
blob: a9cf867b6eb2239d9c1f95f397c9aa3282813ce8 (plain) (blame)
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
import { fail, redirect } from '@sveltejs/kit';
import { login, fd } from '$lib/utils';

export const load = async ({ parent }) => {
	let { user } = await parent();
	if (user?.pubkey) throw redirect(307, `/${user.username}`);
};

export const actions = {
	default: async ({ cookies, request }) => {
		let form = await fd(request);
		let { username, password, token, loginRedirect } = form;
		let user = { username, password, token };

		if (loginRedirect === 'undefined') loginRedirect = undefined;

		try {
			let r = await login(user, cookies, request.headers.get('cf-connecting-ip'));
		} catch (e) {
			return fail(400, { error: 'Login failed', message: e.message, ...form });
		}

		throw redirect(303, loginRedirect || `/${user.username}`);
	}
};