[READ-ONLY] Mirror of https://github.com/flo-bit/svelte-cloudflare-statusphere.
statusphere.atmo.tools
atproto
cloudflare-workers
oauth
svelte
2.0 kB
64 lines
1import { AppBskyActorDefs } from '@atcute/bluesky';
2import type { ActorIdentifier, Did } from '@atcute/lexicons';
3import { page } from '$app/state';
4
5export const user = {
6 get profile() {
7 return (page.data?.profile as AppBskyActorDefs.ProfileViewDetailed | null) ?? null;
8 },
9 get isLoggedIn() {
10 return !!page.data?.did;
11 },
12 get did() {
13 return (page.data?.did as Did | null) ?? null;
14 }
15};
16
17export async function login(handle: string) {
18 if (handle.startsWith('did:')) {
19 if (handle.length < 6) throw new Error('DID must be at least 6 characters');
20 } else if (handle.includes('.') && handle.length > 3) {
21 handle = (handle.startsWith('@') ? handle.slice(1) : handle) as ActorIdentifier;
22 if (handle.length < 4) throw new Error('Handle must be at least 4 characters');
23 } else if (handle.length > 3) {
24 handle = ((handle.startsWith('@') ? handle.slice(1) : handle) +
25 '.bsky.social') as ActorIdentifier;
26 } else {
27 throw new Error('Please provide a valid handle or DID.');
28 }
29
30 const { oauthLogin } = await import('./server/oauth.remote');
31 const { url } = await oauthLogin({ handle });
32 window.location.assign(url);
33
34 // Wait for navigation (prevents UI flash)
35 await new Promise((_resolve, reject) => {
36 window.addEventListener('pageshow', () => reject(new Error('user aborted the login request')), {
37 once: true
38 });
39 });
40}
41
42export async function signup() {
43 const { oauthLogin } = await import('./server/oauth.remote');
44 const { url } = await oauthLogin({ signup: true });
45 window.location.assign(url);
46
47 await new Promise((_resolve, reject) => {
48 window.addEventListener('pageshow', () => reject(new Error('user aborted the signup request')), {
49 once: true
50 });
51 });
52}
53
54export async function logout() {
55 try {
56 const { oauthLogout } = await import('./server/oauth.remote');
57 await oauthLogout();
58 } catch (e) {
59 console.error('Error logging out:', e);
60 }
61
62 // Full reload to clear server session state
63 window.location.href = '/';
64}