mushroom tcg on atproto

*preparing to fight for my life against OAURTH*

+38
+38
frontend/src/lib/utils/atproto.ts
··· 1 + import { 2 + configureOAuth, 3 + createAuthorizationUrl, 4 + resolveFromIdentity 5 + } from '@atcute/oauth-browser-client'; 6 + 7 + async function initializeSession() { 8 + configureOAuth({ 9 + metadata: { 10 + client_id: 'http://127.0.0.1/client-metadata.json', 11 + redirect_uri: 'https://example.com/oauth/callback' 12 + } 13 + }); 14 + 15 + const { identity, metadata } = await resolveFromIdentity('mary.my.id'); 16 + 17 + const authUrl = await createAuthorizationUrl({ 18 + metadata: metadata, 19 + identity: identity, 20 + scope: 'atproto transition:generic transition:chat.bsky' 21 + }); 22 + 23 + await sleep(200); 24 + 25 + // redirect the user to sign in and authorize the app 26 + window.location.assign(authUrl); 27 + 28 + // if this is on an async function, ideally the function should never ever resolve. 29 + // the only way it should resolve at this point is if the user aborted the authorization 30 + // by returning back to this page (thanks to back-forward page caching) 31 + await new Promise((_resolve, reject) => { 32 + const listener = () => { 33 + reject(new Error(`user aborted the login request`)); 34 + }; 35 + 36 + window.addEventListener('pageshow', listener, { once: true }); 37 + }); 38 + }