import type { Me } from '@sailfork/client';
import { clearBoot } from './boot.js';
import { ApiFailure, request } from './http.js';
export const identity = $state<{ me: Me | null }>({ me: null });
// The signed-in user's profile page.
export const myProfile = () => `/${identity.me?.handle ?? ''}`;
// The signed-in user, or null when there is no session.
export async function loadMe(): Promise<Me | null> {
try {
identity.me = await request<Me>('GET', '/api/me');
} catch (err) {
if (!(err instanceof ApiFailure) || err.status !== 401) throw err;
identity.me = null;
}
return identity.me;
}
export async function signOut() {
await request<void>('POST', '/api/auth/sign-out');
clearBoot();
identity.me = null;
}
// Where signing in lands when no page asked: the person's own profile, which every account has.
export async function landing(): Promise<string> {
const me = await loadMe().catch(() => null);
return me ? `/${me.handle}` : '/';
}