move state set logic to utils, add session refreshing / user did storage

This commit is contained in:
Badtz 2025-05-14 10:53:53 -07:00
parent 26ab74c3ad
commit 5d394c16df
3 changed files with 46 additions and 38 deletions

View file

@ -7,6 +7,10 @@ import {
XrpcHandleResolver
} from '@atcute/identity-resolver';
import { userState } from '$lib/state.svelte';
import { OAuthUserAgent, type Session } from '@atcute/oauth-browser-client';
import { configureOAuth } from '@atcute/oauth-browser-client';
import type { At, MySpoorLogActivity } from '@atcute/client/lexicons';
@ -168,3 +172,16 @@ export async function config() {
}
});
}
export async function setState(session: Session) {
const agentItem = new OAuthUserAgent(session);
const rpcItem = new XRPC({ handler: agentItem });
const did = (await agentItem.getSession())?.info.sub ?? '';
const handle = (await resolveHandle(did)) ?? did;
userState.did = did;
userState.handle = handle;
userState.rpc = rpcItem;
userState.agent = agentItem;
return did;
}

View file

@ -11,12 +11,11 @@
} from '@atcute/oauth-browser-client';
import { style, userState } from '$lib/state.svelte';
import { config } from '$lib/util';
import { config, setState } from '$lib/util';
import { onMount } from 'svelte';
async function login(handle: string) {
console.log('login', handle);
config();
const { identity, metadata } = await resolveFromIdentity(handle);
const authUrl = await createAuthorizationUrl({
@ -37,7 +36,6 @@
async function logout() {
console.log('logout');
config();
try {
const session = await getSession(`did:${userState.did}:default`, { allowStale: true });
const agent = new OAuthUserAgent(session);
@ -49,6 +47,7 @@
userState.rpc = undefined;
userState.agent = undefined;
userState.did = undefined;
localStorage.removeItem('sessionDID');
}
async function toggleTheme() {
@ -61,25 +60,20 @@
}
onMount(async () => {
console.log('mounted');
config();
// if (userState.rpc == undefined) {
// console.log('userState.rpc is undefined, attempting to get session');
// try {
// const session = await getSession(`did:${userState.did}`, { allowStale: true });
// console.log('session', session);
// const agent = new OAuthUserAgent(session);
// const rpc = new XRPC({ handler: agent });
// userState.agent = agent;
// userState.rpc = rpc;
// const sessionInfo = await agent.getSession();
// userState.did = sessionInfo?.info.sub ?? '';
// } catch (err) {
// console.error('Error getting session:', err);
// }
// } else {
// console.log('userState.rpc is undefined');
// }
await config();
const savedDid = localStorage.getItem('sessionDID');
if (savedDid) {
try {
const session = await getSession(savedDid as `did:${string}:${string}`, {
allowStale: true
});
setState(session);
} catch (err) {
console.error('Error getting session:', err);
localStorage.removeItem('sessionDID');
}
}
});
</script>

View file

@ -1,27 +1,24 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { userState } from '$lib/state.svelte';
import { config, resolveHandle } from '$lib/util';
import { XRPC } from '@atcute/client';
import { OAuthUserAgent, finalizeAuthorization } from '@atcute/oauth-browser-client';
import { config, setState } from '$lib/util';
import { finalizeAuthorization } from '@atcute/oauth-browser-client';
import { onMount } from 'svelte';
onMount(async () => {
config();
const params = new URLSearchParams(location.hash.slice(1));
try {
config();
const params = new URLSearchParams(location.hash.slice(1));
history.replaceState(null, '', location.pathname + location.search);
history.replaceState(null, '', location.pathname + location.search);
const session = await finalizeAuthorization(params);
const did = await setState(session);
const session = await finalizeAuthorization(params);
const agentItem = new OAuthUserAgent(session);
const rpcItem = new XRPC({ handler: agentItem });
localStorage.setItem('sessionDID', did);
userState.did = (await agentItem?.getSession())?.info.sub ?? '';
userState.handle = (await resolveHandle(userState.did)) ?? userState.did;
userState.rpc = rpcItem;
userState.agent = agentItem;
goto('/');
goto('/');
} catch (err) {
console.error('auth init failed', err);
}
});
</script>