diff --git a/packages/hypergraph-react/src/HypergraphAppContext.tsx b/packages/hypergraph-react/src/HypergraphAppContext.tsx index 6892fd10..4ac41a0a 100644 --- a/packages/hypergraph-react/src/HypergraphAppContext.tsx +++ b/packages/hypergraph-react/src/HypergraphAppContext.tsx @@ -135,29 +135,10 @@ export function HypergraphAppProvider({ return; } const accountId = getAddress(address); - const keys = Identity.loadKeys(storage, accountId); - let authData: { - accountId: Address; - sessionToken: string; - keys: Identity.IdentityKeys; - }; - const location = { + await Identity.login(signer, accountId, syncServerUri, chainId, storage, { host: window.location.host, origin: window.location.origin, - }; - if (!keys && !(await Identity.identityExists(accountId, syncServerUri))) { - authData = await Identity.signup(signer, accountId, syncServerUri, chainId, storage, location); - } else if (keys) { - authData = await Identity.loginWithKeys(keys, accountId, syncServerUri, chainId, storage, location); - } else { - authData = await Identity.loginWithWallet(signer, accountId, syncServerUri, chainId, storage, location); - } - console.log('Identity initialized'); - store.send({ - ...authData, - type: 'setAuth', }); - store.send({ type: 'reset' }); }, [storage, syncServerUri, chainId], ); @@ -167,13 +148,7 @@ export function HypergraphAppProvider({ setWebsocketConnection(undefined); const accountIdToLogout = accountId ?? Identity.loadAccountId(storage); - Identity.wipeAccountId(storage); - if (!accountIdToLogout) { - return; - } - Identity.wipeKeys(storage, accountIdToLogout); - Identity.wipeSyncServerSessionToken(storage, accountIdToLogout); - store.send({ type: 'resetAuth' }); + Identity.logout(accountIdToLogout, storage); }, [accountId, storage, websocketConnection]); const setIdentityAndSessionToken = useCallback( diff --git a/packages/hypergraph/src/identity/index.ts b/packages/hypergraph/src/identity/index.ts index 45161bd5..036614e8 100644 --- a/packages/hypergraph/src/identity/index.ts +++ b/packages/hypergraph/src/identity/index.ts @@ -3,5 +3,6 @@ export * from './create-identity-keys.js'; export * from './get-verified-identity.js'; export * from './identity-encryption.js'; export * from './login.js'; +export * from './logout.js'; export * from './prove-ownership.js'; export * from './types.js'; diff --git a/packages/hypergraph/src/identity/login.ts b/packages/hypergraph/src/identity/login.ts index ae9cdc6d..dd06f3fa 100644 --- a/packages/hypergraph/src/identity/login.ts +++ b/packages/hypergraph/src/identity/login.ts @@ -3,6 +3,7 @@ import { SiweMessage } from 'siwe'; import type { Address, Hex } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; import * as Messages from '../messages/index.js'; +import { store } from '../store.js'; import { loadKeys, loadSyncServerSessionToken, @@ -246,3 +247,32 @@ export async function loginWithKeys( keys, }; } + +export async function login( + signer: Signer, + accountId: Address, + syncServerUri: string, + chainId: number, + storage: Storage, + location: { host: string; origin: string }, +) { + const keys = loadKeys(storage, accountId); + let authData: { + accountId: Address; + sessionToken: string; + keys: IdentityKeys; + }; + if (!keys && !(await identityExists(accountId, syncServerUri))) { + authData = await signup(signer, accountId, syncServerUri, chainId, storage, location); + } else if (keys) { + authData = await loginWithKeys(keys, accountId, syncServerUri, chainId, storage, location); + } else { + authData = await loginWithWallet(signer, accountId, syncServerUri, chainId, storage, location); + } + console.log('Identity initialized'); + store.send({ + ...authData, + type: 'setAuth', + }); + store.send({ type: 'reset' }); +} diff --git a/packages/hypergraph/src/identity/logout.ts b/packages/hypergraph/src/identity/logout.ts new file mode 100644 index 00000000..70d11b37 --- /dev/null +++ b/packages/hypergraph/src/identity/logout.ts @@ -0,0 +1,13 @@ +import { store } from './../store.js'; +import { wipeAccountId, wipeKeys, wipeSyncServerSessionToken } from './auth-storage.js'; +import type { Storage } from './types.js'; + +export function logout(accountId: string | null, storage: Storage) { + wipeAccountId(storage); + if (!accountId) { + return; + } + wipeKeys(storage, accountId); + wipeSyncServerSessionToken(storage, accountId); + store.send({ type: 'resetAuth' }); +}