diff options
| author | Jurjen de Vries <[email protected]> | 2025-09-11 00:48:15 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-11 00:48:15 +0200 |
| commit | 0c69c9848c6b98c92609d209103efc60003c9723 (patch) | |
| tree | 445121def885f13d0f80c06864ad2ff4d5ef95a1 /src/lib/utils.ts | |
| parent | 2abcf6e435fca5d86960f3ba4228002bfb0a19e6 (diff) | |
| parent | 9277b2921e09ee9dd5f39cf4af30a2330f4d95d1 (diff) | |
| download | coinos-ui-0c69c9848c6b98c92609d209103efc60003c9723.tar.gz coinos-ui-0c69c9848c6b98c92609d209103efc60003c9723.zip | |
Merge branch 'coinos:master' into master
Diffstat (limited to 'src/lib/utils.ts')
| -rw-r--r-- | src/lib/utils.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 2e1bb840..28a219e1 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -3,6 +3,7 @@ import { goto } from "$app/navigation"; import { page } from "$app/stores"; import { PUBLIC_COINOS_NETWORK, PUBLIC_COINOS_URL } from "$env/static/public"; import { bytesToHex } from "@noble/hashes/utils"; +import { fail as svelteFail, redirect } from "@sveltejs/kit"; import { toast } from "@zerodevx/svelte-toast"; import { differenceInDays, @@ -456,3 +457,34 @@ export const ago = (t) => { const days = differenceInDays(now, date); return `${days}d`; }; + +export const register = async (user, ip, cookies, loginRedirect) => { + let error; + + 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 svelteFail(400, { error }); + redirect(303, loginRedirect || `/${user.username}`); +}; |
