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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import { fd, get, login, post } from "$lib/utils";
import { fail, redirect } from "@sveltejs/kit";
export const load = async ({ parent }) => {
const { user } = await parent();
if (user) redirect(307, `/${user.username}`);
const index = Math.floor(Math.random() * 64) + 1;
const { challenge } = await get("/challenge");
return { index, challenge };
};
export const actions = {
default: async ({ cookies, request }) => {
const ip = request.headers.get("cf-connecting-ip");
const form = await fd(request);
let { picture, username, password, pubkey, loginRedirect } = form;
const user = { picture, username, password, pubkey };
let error;
if (loginRedirect === "undefined") loginRedirect = undefined;
let sk;
try {
({ sk } = await post("/register", { user }, { "cf-connecting-ip": ip }));
} catch (e) {
({ message: error } = e as Error);
}
try {
await login(user, cookies, ip);
error = null;
} catch (e) {
const { message } = e as Error;
error ||= message;
}
const expires = new Date();
expires.setSeconds(expires.getSeconds() + 21000000);
cookies.set("sk", sk, {
path: "/",
expires,
httpOnly: false,
sameSite: "lax",
});
if (error) return fail(400, { error });
redirect(303, loginRedirect || `/${user.username}`);
},
};
|