Skip to content

Commit

Permalink
[native] implement hook for restore
Browse files Browse the repository at this point in the history
Summary:
[ENG-8195](https://linear.app/comm/issue/ENG-8195/implement-new-log-in-for-usernamepassword-users).

Implementing a hook for user restore, similar to the password/wallet user login.

Depends on D14082

Test Plan:
1. Use this hook in `LogInPanel` instead of `usePasswordLogIn`.
2. Add a bunch of logs.
3. Register a new Account and add a different user as a friend.
4. Log out using v2.
5. Log in (which executed restore).
6. Look at logs - all steps passed.
7. The user is visually logged in, and everything works.
8. On other user observer logs, the device list is updated, but it's not passing validation (which is expected because of D14079). After D14079 is deployed, validation should pass.

EDIT: Tested after Identity deploy, it works!
{F3419152}

Reviewers: bartek, tomek, varun

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D14083
  • Loading branch information
xsanm committed Dec 11, 2024
1 parent c20f2b6 commit 6e8a60a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/hooks/login-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ function useSecondaryDeviceLogIn(): (userID: string) => Promise<void> {
);
}

export { usePasswordLogIn, useWalletLogIn, useSecondaryDeviceLogIn };
export { usePasswordLogIn, useWalletLogIn, useSecondaryDeviceLogIn, useLogIn };
44 changes: 43 additions & 1 deletion native/account/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import invariant from 'invariant';
import * as React from 'react';

import { restoreUserActionTypes } from 'lib/actions/user-actions.js';
import { useLogIn } from 'lib/hooks/login-hooks.js';
import { IdentityClientContext } from 'lib/shared/identity-client-context.js';
import type {
IdentityAuthResult,
Expand All @@ -12,6 +14,7 @@ import { getConfig } from 'lib/utils/config.js';
import { getContentSigningKey } from 'lib/utils/crypto-utils.js';
import { composeRawDeviceList } from 'lib/utils/device-list-utils.js';
import { getMessageForException } from 'lib/utils/errors.js';
import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js';
import { useSelector } from 'lib/utils/redux-utils.js';

import { useClientBackup } from '../backup/use-client-backup.js';
Expand Down Expand Up @@ -113,4 +116,43 @@ function useRestoreProtocol(): (
);
}

export { useRestoreProtocol };
function useRestore(): (
userIdentifier: string,
secret: string,
siweMessage?: string,
siweSignature?: string,
) => Promise<void> {
const restoreProtocol = useRestoreProtocol();
const dispatchActionPromise = useDispatchActionPromise();
const restoreAuth = React.useCallback(
(
userIdentifier: string,
secret: string,
siweMessage?: string,
siweSignature?: string,
) => {
const promise = restoreProtocol(
userIdentifier,
secret,
siweMessage,
siweSignature,
);
void dispatchActionPromise(restoreUserActionTypes, promise);
return promise;
},
[dispatchActionPromise, restoreProtocol],
);

const logIn = useLogIn();
return React.useCallback(
(
userIdentifier: string,
secret: string,
siweMessage?: string,
siweSignature?: string,
) => logIn(restoreAuth(userIdentifier, secret, siweMessage, siweSignature)),
[logIn, restoreAuth],
);
}

export { useRestore };

0 comments on commit 6e8a60a

Please sign in to comment.