This repository has no description
1import { goto } from '$app/navigation';
2import { resolve } from '$app/paths';
3import { redirect, type RequestEvent } from '@sveltejs/kit';
4import { CURRENT_DID_KEY, CURRENT_HANDLE_KEY } from './accounts';
5import type { Auth } from '../auth.svelte';
6
7export interface RequireAuthResult {
8 did: string;
9 handle: string;
10}
11
12const loginWithReturn = (returnUrl: string): string =>
13 `/login?return_url=${encodeURIComponent(returnUrl)}`;
14
15export const requireAuth = (event: RequestEvent): RequireAuthResult => {
16 const did = event.cookies.get(CURRENT_DID_KEY);
17 if (!did) {
18 redirect(302, loginWithReturn(event.url.pathname + event.url.search));
19 }
20 return { did, handle: event.cookies.get(CURRENT_HANDLE_KEY) ?? did };
21};
22
23export const requireAuthClient = (auth: Auth, url: URL): boolean => {
24 if (auth.currentDid) return true;
25 void goto(resolve(loginWithReturn(url.pathname + url.search) as '/login'), {
26 replaceState: true
27 });
28 return false;
29};