Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions packages/hypergraph-react/src/HypergraphAppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/hypergraph/src/identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 30 additions & 0 deletions packages/hypergraph/src/identity/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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' });
}
13 changes: 13 additions & 0 deletions packages/hypergraph/src/identity/logout.ts
Original file line number Diff line number Diff line change
@@ -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' });
}
Loading