diff options
| author | Adam Soltys <[email protected]> | 2026-02-06 17:22:01 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-06 17:22:01 +0000 |
| commit | 8ec5dda44f4676a2e5dae609792710b0719cec43 (patch) | |
| tree | b018ac129dce268ffd0f355f379c1be0f693a1e9 /src | |
| parent | c9a73999ba678955a3017c4d8442ec4e0b60a0d1 (diff) | |
| download | coinos-ui-8ec5dda44f4676a2e5dae609792710b0719cec43.tar.gz coinos-ui-8ec5dda44f4676a2e5dae609792710b0719cec43.zip | |
fix tor login
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/utils.ts | 25 | ||||
| -rw-r--r-- | src/routes/(static)/login/+page.server.ts | 9 | ||||
| -rw-r--r-- | src/routes/(static)/register/+page.server.ts | 3 |
3 files changed, 24 insertions, 13 deletions
diff --git a/src/lib/utils.ts b/src/lib/utils.ts index b981a4d4..bc5df67a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -169,24 +169,31 @@ export const login = async ( }, cookies, ip: string, + host?: string, ) => { const maxAge = 380 * 24 * 60 * 60; + const headers: Record<string, string> = { + "content-type": "application/json", + accept: "application/json", + "cf-connecting-ip": ip, + }; + if (host) headers["host"] = host; + const res = await fetch(`${base}/login`, { method: "POST", body: JSON.stringify(user), - headers: { - "content-type": "application/json", - accept: "application/json", - "cf-connecting-ip": ip, - }, + headers, }); + + const text = await res.text(); + if (res.status === 401) { - const text = await res.text(); if (text.startsWith("2fa")) throw new Error("2fa"); + throw new Error(text); } - const { user: u, token } = await res.json(); + const { user: u, token } = JSON.parse(text); if (!token) throw new Error("Login failed"); const expires = new Date(); @@ -490,7 +497,7 @@ export const ago = (t) => { return `${days}d`; }; -export const register = async (user, ip, cookies, loginRedirect) => { +export const register = async (user, ip, cookies, loginRedirect, host?) => { let error; let sk; @@ -501,7 +508,7 @@ export const register = async (user, ip, cookies, loginRedirect) => { } try { - await login(user, cookies, ip); + await login(user, cookies, ip, host); error = null; } catch (e) { const { message } = e as Error; diff --git a/src/routes/(static)/login/+page.server.ts b/src/routes/(static)/login/+page.server.ts index d81f2ccb..6c82a209 100644 --- a/src/routes/(static)/login/+page.server.ts +++ b/src/routes/(static)/login/+page.server.ts @@ -25,7 +25,7 @@ export const actions = { if (loginRedirect === "undefined") loginRedirect = undefined; try { - await login(user, cookies, request.headers.get("cf-connecting-ip")); + await login(user, cookies, request.headers.get("cf-connecting-ip"), request.headers.get("host")); } catch (e) { const { message } = e as Error; return fail(400, { error: "Login failed", message, ...form }); @@ -49,15 +49,18 @@ export const actions = { "content-type": "application/json", accept: "application/json", "cf-connecting-ip": request.headers.get("cf-connecting-ip"), + host: request.headers.get("host"), }, }); + const text = await res.text(); + if (res.status === 401) { - const text = await res.text(); if (text.startsWith("2fa")) throw new Error("2fa"); + throw new Error(text); } - const { user, token } = await res.json(); + const { user, token } = JSON.parse(text); if (!token) throw new Error("Login failed"); const { username, language } = user; diff --git a/src/routes/(static)/register/+page.server.ts b/src/routes/(static)/register/+page.server.ts index d2dbe299..2bcf6f2d 100644 --- a/src/routes/(static)/register/+page.server.ts +++ b/src/routes/(static)/register/+page.server.ts @@ -14,12 +14,13 @@ export const load = async ({ parent }) => { export const actions = { default: async ({ cookies, request }) => { const ip = request.headers.get("cf-connecting-ip"); + const host = request.headers.get("host"); const form = await fd(request); const { picture, username, password, challenge, recaptcha } = form; let { loginRedirect } = form; if (loginRedirect === "undefined") loginRedirect = undefined; const user = { picture, username, password, challenge, recaptcha }; - return register(user, ip, cookies, loginRedirect); + return register(user, ip, cookies, loginRedirect, host); }, }; |
