aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(static)/login/+page.server.ts
blob: 6e268c926925f3b1731edab0da6ce4b2e1ba2839 (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
26
import { fd, login } from "$lib/utils";
import { fail, redirect } from "@sveltejs/kit";

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

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

		if (loginRedirect === "undefined") loginRedirect = undefined;

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

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