Skip to content

Commit 63e0371

Browse files
authored
move login & logout to core (#147)
1 parent ead20a9 commit 63e0371

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,10 @@ export function HypergraphAppProvider({
135135
return;
136136
}
137137
const accountId = getAddress(address);
138-
const keys = Identity.loadKeys(storage, accountId);
139-
let authData: {
140-
accountId: Address;
141-
sessionToken: string;
142-
keys: Identity.IdentityKeys;
143-
};
144-
const location = {
138+
await Identity.login(signer, accountId, syncServerUri, chainId, storage, {
145139
host: window.location.host,
146140
origin: window.location.origin,
147-
};
148-
if (!keys && !(await Identity.identityExists(accountId, syncServerUri))) {
149-
authData = await Identity.signup(signer, accountId, syncServerUri, chainId, storage, location);
150-
} else if (keys) {
151-
authData = await Identity.loginWithKeys(keys, accountId, syncServerUri, chainId, storage, location);
152-
} else {
153-
authData = await Identity.loginWithWallet(signer, accountId, syncServerUri, chainId, storage, location);
154-
}
155-
console.log('Identity initialized');
156-
store.send({
157-
...authData,
158-
type: 'setAuth',
159141
});
160-
store.send({ type: 'reset' });
161142
},
162143
[storage, syncServerUri, chainId],
163144
);
@@ -167,13 +148,7 @@ export function HypergraphAppProvider({
167148
setWebsocketConnection(undefined);
168149

169150
const accountIdToLogout = accountId ?? Identity.loadAccountId(storage);
170-
Identity.wipeAccountId(storage);
171-
if (!accountIdToLogout) {
172-
return;
173-
}
174-
Identity.wipeKeys(storage, accountIdToLogout);
175-
Identity.wipeSyncServerSessionToken(storage, accountIdToLogout);
176-
store.send({ type: 'resetAuth' });
151+
Identity.logout(accountIdToLogout, storage);
177152
}, [accountId, storage, websocketConnection]);
178153

179154
const setIdentityAndSessionToken = useCallback(

packages/hypergraph/src/identity/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from './create-identity-keys.js';
33
export * from './get-verified-identity.js';
44
export * from './identity-encryption.js';
55
export * from './login.js';
6+
export * from './logout.js';
67
export * from './prove-ownership.js';
78
export * from './types.js';

packages/hypergraph/src/identity/login.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SiweMessage } from 'siwe';
33
import type { Address, Hex } from 'viem';
44
import { privateKeyToAccount } from 'viem/accounts';
55
import * as Messages from '../messages/index.js';
6+
import { store } from '../store.js';
67
import {
78
loadKeys,
89
loadSyncServerSessionToken,
@@ -246,3 +247,32 @@ export async function loginWithKeys(
246247
keys,
247248
};
248249
}
250+
251+
export async function login(
252+
signer: Signer,
253+
accountId: Address,
254+
syncServerUri: string,
255+
chainId: number,
256+
storage: Storage,
257+
location: { host: string; origin: string },
258+
) {
259+
const keys = loadKeys(storage, accountId);
260+
let authData: {
261+
accountId: Address;
262+
sessionToken: string;
263+
keys: IdentityKeys;
264+
};
265+
if (!keys && !(await identityExists(accountId, syncServerUri))) {
266+
authData = await signup(signer, accountId, syncServerUri, chainId, storage, location);
267+
} else if (keys) {
268+
authData = await loginWithKeys(keys, accountId, syncServerUri, chainId, storage, location);
269+
} else {
270+
authData = await loginWithWallet(signer, accountId, syncServerUri, chainId, storage, location);
271+
}
272+
console.log('Identity initialized');
273+
store.send({
274+
...authData,
275+
type: 'setAuth',
276+
});
277+
store.send({ type: 'reset' });
278+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { store } from './../store.js';
2+
import { wipeAccountId, wipeKeys, wipeSyncServerSessionToken } from './auth-storage.js';
3+
import type { Storage } from './types.js';
4+
5+
export function logout(accountId: string | null, storage: Storage) {
6+
wipeAccountId(storage);
7+
if (!accountId) {
8+
return;
9+
}
10+
wipeKeys(storage, accountId);
11+
wipeSyncServerSessionToken(storage, accountId);
12+
store.send({ type: 'resetAuth' });
13+
}

0 commit comments

Comments
 (0)