replies timeline only, appview-less bluesky client
at main 1.3 kB view raw
1import { replaceState } from '$app/navigation'; 2import { addAccount, loggingIn } from '$lib/accounts'; 3import { AtpClient } from '$lib/at/client'; 4import { flow, sessions } from '$lib/at/oauth'; 5import { err, ok, type Result } from '$lib/result'; 6import type { PageLoad } from './$types'; 7 8export type PageProps = { 9 data: { 10 client: Result<AtpClient | null, string>; 11 }; 12}; 13 14export const load: PageLoad = async (): Promise<PageProps['data']> => { 15 return { client: await handleLogin() }; 16}; 17 18const handleLogin = async (): Promise<Result<AtpClient | null, string>> => { 19 const account = loggingIn.get(); 20 if (!account) return ok(null); 21 22 const currentUrl = new URL(window.location.href); 23 // scrub history so auth state cant be replayed 24 try { 25 replaceState('', '/'); 26 } catch { 27 // if router was unitialized then we probably dont need to scrub anyway 28 // so its fine 29 } 30 31 loggingIn.set(null); 32 await sessions.remove(account.did); 33 const agent = await flow.finalize(currentUrl); 34 if (!agent.ok || !agent.value) { 35 if (!agent.ok) return err(agent.error); 36 return err('no session was logged into?!'); 37 } 38 39 const client = new AtpClient(); 40 const result = await client.login(agent.value); 41 if (!result.ok) return err(result.error); 42 43 addAccount(account); 44 return ok(client); 45};