fix: got correct order for restoring an account with fetch

pull/3056/head
William Grant 1 year ago
parent 1b405f533e
commit e0ca888e85

@ -1,13 +1,14 @@
import { AnyAction, Dispatch } from '@reduxjs/toolkit';
import { AnyAction } from '@reduxjs/toolkit';
import { isEmpty } from 'lodash';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { ONBOARDING_TIMES } from '../../../session/constants';
import { trigger } from '../../../shims/events';
import {
AccountRestoration,
resetOnboardingState,
setAccountRestorationStep,
} from '../../../state/onboarding/ducks/registration';
import { registrationDone } from '../../../util/accountManager';
let interval: NodeJS.Timeout;
@ -15,8 +16,8 @@ type UseRecoveryProgressEffectProps = {
step: AccountRestoration;
progress: number;
setProgress: (progress: number) => AnyAction;
ourPubkey: string;
displayName: string;
dispatch: Dispatch;
};
/**
@ -24,11 +25,13 @@ type UseRecoveryProgressEffectProps = {
* @param step AccountRestoration the onboarding step we are currently on
* @param progress number the progress of the loading bar
* @param setProgress (progress: number) => AnyAction redux function to set the progress of the loading bar
* @param displayName string the display name of the user
* @param dispatch
* @param ourPubkey: string the public key of the user
* @param displayName: string the display name of the user
*/
export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps) => {
const { step, progress, setProgress, displayName, dispatch } = props;
const { step, progress, setProgress, ourPubkey, displayName } = props;
const dispatch = useDispatch();
useEffect(() => {
if (step === AccountRestoration.Loading) {
@ -36,9 +39,6 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
if (progress < 100) {
dispatch(setProgress(progress + 1));
}
// window.log.debug(
// `WIP: [continueYourSession] AccountRestoration.Loading Loading progress ${progress}%`
// );
if (progress >= 100) {
clearInterval(interval);
@ -56,9 +56,6 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
if (progress < 100) {
dispatch(setProgress(progress + 1));
}
window.log.debug(
`WIP: [useRecoveryProgressEffect] AccountRestoration. Finishing progress ${progress}%`
);
if (progress >= 100) {
clearInterval(interval);
@ -78,7 +75,7 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
} else {
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
window.log.debug(
`WIP: [useRecoveryProgressEffect] AccountRestoration.DisplayName failed to fetch display name so we need to enter it manually`
`WIP: [onboarding] AccountRestoration.DisplayName failed to fetch display name so we need to enter it manually`
);
}
}, ONBOARDING_TIMES.RECOVERY_FINISHED);
@ -86,15 +83,21 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
if (step === AccountRestoration.Complete) {
clearInterval(interval);
if (!isEmpty(displayName)) {
if (!isEmpty(ourPubkey) && !isEmpty(displayName)) {
window.log.debug(
`WIP: [onboarding] AccountRestoration.Complete opening inbox for ${displayName}`
);
// eslint-disable-next-line more/no-then
void registrationDone(ourPubkey, displayName).then(() => trigger('openInbox'));
} else {
window.log.debug(
`WIP: [useRecoveryProgressEffect] AccountRestoration.Complete opening inbox for ${displayName}`
`WIP: [onboarding] AccountRestoration.Complete failed to find the pubkey and display name`
);
dispatch(resetOnboardingState());
trigger('openInbox');
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
}
}
return () => clearInterval(interval);
}, [dispatch, displayName, progress, setProgress, step]);
}, [dispatch, displayName, ourPubkey, progress, setProgress, step]);
};

@ -1,3 +1,4 @@
import { Dispatch } from '@reduxjs/toolkit';
import { useDispatch } from 'react-redux';
import { ONBOARDING_TIMES } from '../../../session/constants';
import { InvalidWordsError, NotEnoughWordsError } from '../../../session/crypto/mnemonic';
@ -9,6 +10,7 @@ import {
setAccountRestorationStep,
setDisplayName,
setDisplayNameError,
setHexGeneratedPubKey,
setProgress,
setRecoveryPassword,
setRecoveryPasswordError,
@ -17,15 +19,12 @@ import {
useDisplayName,
useDisplayNameError,
useOnboardAccountRestorationStep,
useOnboardHexGeneratedPubKey,
useProgress,
useRecoveryPassword,
useRecoveryPasswordError,
} from '../../../state/onboarding/selectors/registration';
import {
registerSingleDevice,
registrationDone,
signInByLinkingDevice,
} from '../../../util/accountManager';
import { registerSingleDevice, signInByLinkingDevice } from '../../../util/accountManager';
import { setSignInByLinking, setSignWithRecoveryPhrase } from '../../../util/storage';
import { Flex } from '../../basic/Flex';
import { SessionButton, SessionButtonColor } from '../../basic/SessionButton';
@ -53,6 +52,7 @@ async function signInWithNewDisplayName(signInDetails: RecoverDetails) {
await resetRegistration();
await registerSingleDevice(recoveryPassword, 'english', trimName);
await setSignInByLinking(false);
await setSignWithRecoveryPhrase(true);
} catch (e) {
await resetRegistration();
@ -71,13 +71,10 @@ async function signInAndFetchDisplayName(
signInDetails: RecoverDetails & {
/** this is used to trigger the loading animation further down the registration pipeline */
loadingAnimationCallback: () => void;
}
},
dispatch: Dispatch
) {
const { recoveryPassword, loadingAnimationCallback } = signInDetails;
window.log.debug(`WIP: [signInAndFetchDisplayName] starting sign in....`);
let displayNameFromNetwork = '';
let ourPubkey = '';
try {
await resetRegistration();
@ -90,13 +87,13 @@ async function signInAndFetchDisplayName(
const promiseWait = PromiseUtils.waitForTask(done => {
window.Whisper.events.on(
'configurationMessageReceived',
async (displayName: string, pubkey: string) => {
async (ourPubkey: string, displayName: string) => {
window.Whisper.events.off('configurationMessageReceived');
await setSignInByLinking(false);
await setSignWithRecoveryPhrase(true);
displayNameFromNetwork = displayName;
ourPubkey = pubkey;
await setSignInByLinking(true);
await setSignWithRecoveryPhrase(false);
dispatch(setHexGeneratedPubKey(ourPubkey));
dispatch(setDisplayName(displayName));
dispatch(setAccountRestorationStep(AccountRestoration.Finishing));
done(displayName);
}
);
@ -107,49 +104,55 @@ async function signInAndFetchDisplayName(
await resetRegistration();
throw e;
}
window.log.debug(
`WIP: [signInAndFetchDisplayName] signed in with displayName: "${displayNameFromNetwork}" and pubkey: "${ourPubkey}`
);
return { displayNameFromNetwork, ourPubkey };
}
export const RestoreAccount = () => {
const step = useOnboardAccountRestorationStep();
const recoveryPassword = useRecoveryPassword();
const recoveryPasswordError = useRecoveryPasswordError();
const ourPubkey = useOnboardHexGeneratedPubKey();
const displayName = useDisplayName();
const displayNameError = useDisplayNameError();
const progress = useProgress();
const dispatch = useDispatch();
useRecoveryProgressEffect({ step, progress, setProgress, displayName, dispatch });
useRecoveryProgressEffect({
step,
progress,
setProgress,
ourPubkey,
displayName,
});
const recoverAndFetchDisplayName = async () => {
if (!(!!recoveryPassword && !recoveryPasswordError)) {
return;
}
dispatch(setProgress(0));
try {
const { displayNameFromNetwork, ourPubkey } = await signInAndFetchDisplayName({
recoveryPassword,
errorCallback: e => {
throw e;
},
loadingAnimationCallback: () => {
dispatch(setAccountRestorationStep(AccountRestoration.Loading));
window.log.debug(
`WIP: [onboarding] restore account: recoverAndFetchDisplayName() is starting recoveryPassword: ${recoveryPassword}`
);
dispatch(setProgress(0));
await signInAndFetchDisplayName(
{
recoveryPassword,
errorCallback: e => {
throw e;
},
loadingAnimationCallback: () => {
dispatch(setAccountRestorationStep(AccountRestoration.Loading));
},
},
});
dispatch(setDisplayName(displayNameFromNetwork));
await registrationDone(ourPubkey, displayName);
dispatch(setAccountRestorationStep(AccountRestoration.Finishing));
dispatch
);
} catch (e) {
window.log.debug(
`WIP: [onboarding] restore account: restoration failed! Error: ${e.message || e}`
);
if (e instanceof NotFoundError || e instanceof TaskTimedOutError) {
window.log.debug(
`WIP: [recoverAndFetchDisplayName] AccountRestoration.RecoveryPassword failed to get a display name so we need to enter it manually. Error: ${e}`
);
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
return;
}
@ -161,9 +164,6 @@ export const RestoreAccount = () => {
} else {
dispatch(setRecoveryPasswordError(window.i18n('recoveryPasswordErrorMessageGeneric')));
}
window.log.debug(
`WIP: [recoverAndFetchDisplayName] exception during registration: ${e.message || e} type ${typeof JSON.stringify(e)}`
);
dispatch(setAccountRestorationStep(AccountRestoration.RecoveryPassword));
}
};
@ -173,8 +173,11 @@ export const RestoreAccount = () => {
return;
}
dispatch(setProgress(0));
try {
window.log.debug(
`WIP: [onboarding] restore account: recoverAndEnterDisplayName() is starting recoveryPassword: ${recoveryPassword} displayName: ${displayName}`
);
dispatch(setProgress(0));
await signInWithNewDisplayName({
displayName,
recoveryPassword,
@ -186,7 +189,7 @@ export const RestoreAccount = () => {
dispatch(setAccountRestorationStep(AccountRestoration.Complete));
} catch (e) {
window.log.debug(
`WIP: [recoverAndEnterDisplayName] AccountRestoration.DisplayName failed to set the display name. Error: ${e}`
`WIP: [onboarding] restore account: restoration with new display name failed! Error: ${e.message || e}`
);
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
}

Loading…
Cancel
Save