aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(static)/login/+page.server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/(static)/login/+page.server.ts')
-rw-r--r--src/routes/(static)/login/+page.server.ts44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/routes/(static)/login/+page.server.ts b/src/routes/(static)/login/+page.server.ts
index 6e268c92..740f02d8 100644
--- a/src/routes/(static)/login/+page.server.ts
+++ b/src/routes/(static)/login/+page.server.ts
@@ -1,13 +1,15 @@
-import { fd, login } from "$lib/utils";
+import { fd, get, 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}`);
+ const { id } = await get("/challenge");
+ return { id };
};
export const actions = {
- default: async ({ cookies, request }) => {
+ login: async ({ cookies, request }) => {
const form = await fd(request);
let { username, password, token, loginRedirect } = form;
const user = { username, password, token };
@@ -23,4 +25,42 @@ export const actions = {
redirect(307, loginRedirect || `/${user.username}`);
},
+
+ nostr: async ({ cookies, fetch, request }) => {
+ const form = await fd(request);
+ let { id, event, loginRedirect } = form;
+ if (loginRedirect === "null") loginRedirect = undefined;
+ event = JSON.parse(event);
+
+ const maxAge = 380 * 24 * 60 * 60;
+
+ const res = await fetch("/api/nostrLogin", {
+ method: "POST",
+ body: JSON.stringify({ id, event }),
+ headers: {
+ "content-type": "application/json",
+ accept: "application/json",
+ "cf-connecting-ip": request.headers.get("cf-connecting-ip"),
+ },
+ });
+
+ if (res.status === 401) {
+ const text = await res.text();
+ if (text.startsWith("2fa")) throw new Error("2fa");
+ }
+
+ const { user, token } = await res.json();
+ if (!token) throw new Error("Login failed");
+ const { username, language } = user;
+
+ const expires = new Date();
+ expires.setSeconds(expires.getSeconds() + maxAge);
+
+ const opts = { path: "/", expires };
+ if (language) cookies.set("lang", language, opts);
+ cookies.set("username", username, opts);
+ cookies.set("token", token, opts);
+
+ redirect(307, loginRedirect || `/${username}`);
+ },
};