move state set logic to utils, add session refreshing / user did storage
This commit is contained in:
parent
26ab74c3ad
commit
5d394c16df
3 changed files with 46 additions and 38 deletions
|
|
@ -7,6 +7,10 @@ import {
|
||||||
XrpcHandleResolver
|
XrpcHandleResolver
|
||||||
} from '@atcute/identity-resolver';
|
} 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 { configureOAuth } from '@atcute/oauth-browser-client';
|
||||||
|
|
||||||
import type { At, MySpoorLogActivity } from '@atcute/client/lexicons';
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,11 @@
|
||||||
} from '@atcute/oauth-browser-client';
|
} from '@atcute/oauth-browser-client';
|
||||||
|
|
||||||
import { style, userState } from '$lib/state.svelte';
|
import { style, userState } from '$lib/state.svelte';
|
||||||
import { config } from '$lib/util';
|
import { config, setState } from '$lib/util';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
async function login(handle: string) {
|
async function login(handle: string) {
|
||||||
console.log('login', handle);
|
console.log('login', handle);
|
||||||
config();
|
|
||||||
|
|
||||||
const { identity, metadata } = await resolveFromIdentity(handle);
|
const { identity, metadata } = await resolveFromIdentity(handle);
|
||||||
const authUrl = await createAuthorizationUrl({
|
const authUrl = await createAuthorizationUrl({
|
||||||
|
|
@ -37,7 +36,6 @@
|
||||||
|
|
||||||
async function logout() {
|
async function logout() {
|
||||||
console.log('logout');
|
console.log('logout');
|
||||||
config();
|
|
||||||
try {
|
try {
|
||||||
const session = await getSession(`did:${userState.did}:default`, { allowStale: true });
|
const session = await getSession(`did:${userState.did}:default`, { allowStale: true });
|
||||||
const agent = new OAuthUserAgent(session);
|
const agent = new OAuthUserAgent(session);
|
||||||
|
|
@ -49,6 +47,7 @@
|
||||||
userState.rpc = undefined;
|
userState.rpc = undefined;
|
||||||
userState.agent = undefined;
|
userState.agent = undefined;
|
||||||
userState.did = undefined;
|
userState.did = undefined;
|
||||||
|
localStorage.removeItem('sessionDID');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function toggleTheme() {
|
async function toggleTheme() {
|
||||||
|
|
@ -61,25 +60,20 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
console.log('mounted');
|
await config();
|
||||||
config();
|
const savedDid = localStorage.getItem('sessionDID');
|
||||||
// if (userState.rpc == undefined) {
|
if (savedDid) {
|
||||||
// console.log('userState.rpc is undefined, attempting to get session');
|
try {
|
||||||
// try {
|
const session = await getSession(savedDid as `did:${string}:${string}`, {
|
||||||
// const session = await getSession(`did:${userState.did}`, { allowStale: true });
|
allowStale: true
|
||||||
// console.log('session', session);
|
});
|
||||||
// const agent = new OAuthUserAgent(session);
|
|
||||||
// const rpc = new XRPC({ handler: agent });
|
setState(session);
|
||||||
// userState.agent = agent;
|
} catch (err) {
|
||||||
// userState.rpc = rpc;
|
console.error('Error getting session:', err);
|
||||||
// const sessionInfo = await agent.getSession();
|
localStorage.removeItem('sessionDID');
|
||||||
// userState.did = sessionInfo?.info.sub ?? '';
|
}
|
||||||
// } catch (err) {
|
}
|
||||||
// console.error('Error getting session:', err);
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// console.log('userState.rpc is undefined');
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,24 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { userState } from '$lib/state.svelte';
|
import { config, setState } from '$lib/util';
|
||||||
import { config, resolveHandle } from '$lib/util';
|
import { finalizeAuthorization } from '@atcute/oauth-browser-client';
|
||||||
import { XRPC } from '@atcute/client';
|
|
||||||
import { OAuthUserAgent, finalizeAuthorization } from '@atcute/oauth-browser-client';
|
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
try {
|
||||||
config();
|
config();
|
||||||
const params = new URLSearchParams(location.hash.slice(1));
|
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 session = await finalizeAuthorization(params);
|
||||||
const agentItem = new OAuthUserAgent(session);
|
const did = await setState(session);
|
||||||
const rpcItem = new XRPC({ handler: agentItem });
|
|
||||||
|
|
||||||
userState.did = (await agentItem?.getSession())?.info.sub ?? '';
|
localStorage.setItem('sessionDID', did);
|
||||||
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>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue