aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(static)/login/+page.server.ts
blob: 268f95b784bbcd9e3a3e86397d192de0a2e3301e (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 }) => {
	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: any) {
			return fail(400, { error: "Login failed", message: e.message, ...form });
		}

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