fix: cleaned up typings and flattened error callback

pull/3056/head
William Grant 1 year ago
parent c9eac6c83e
commit 500ab60ffa

@ -17,12 +17,6 @@ import { SessionIcon, SessionIconButton } from '../icon';
import { OnboardContainer } from './components';
import { CreateAccount, RestoreAccount, Start } from './stages';
export type RecoverDetails = {
recoveryPassword: string;
errorCallback: (error: Error) => void;
displayName?: string;
};
const StyledRegistrationContainer = styled(Flex)`
width: 348px;
.session-button {

@ -53,7 +53,7 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
clearInterval(interval);
// if we didn't get the display name in time, we need to enter it manually
window.log.debug(
`WIP: [onboarding] restore account: We failed with a time out when fetching a display, so we had to enter it manually`
`WIP: [onboarding] restore account: We failed with a time out when fetching a display name, so we restored manually`
);
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
}
@ -80,7 +80,7 @@ export const useRecoveryProgressEffect = (props: UseRecoveryProgressEffectProps)
dispatch(setAccountRestorationStep(AccountRestoration.Complete));
} else {
window.log.debug(
`WIP: [onboarding] restore account: We failed with an error when fetching a display name, so we had to enter it manually`
`WIP: [onboarding] restore account: We failed with an error when fetching a display name, so we restored manually`
);
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
}

@ -28,13 +28,17 @@ import { Flex } from '../../basic/Flex';
import { SessionButton, SessionButtonColor } from '../../basic/SessionButton';
import { SpacerLG, SpacerSM } from '../../basic/Text';
import { SessionInput } from '../../inputs';
import { RecoverDetails } from '../RegistrationStages';
import { OnboardDescription, OnboardHeading } from '../components';
import { BackButtonWithininContainer } from '../components/BackButton';
import { displayNameIsValid, resetRegistration, sanitizeDisplayNameOrToast } from '../utils';
async function signUp(signUpDetails: RecoverDetails) {
const { displayName, recoveryPassword, errorCallback } = signUpDetails;
export type AccountDetails = {
recoveryPassword: string;
displayName?: string;
};
async function signUp(signUpDetails: AccountDetails) {
const { displayName, recoveryPassword } = signUpDetails;
try {
const validDisplayName = displayNameIsValid(displayName);
@ -45,7 +49,7 @@ async function signUp(signUpDetails: RecoverDetails) {
trigger('openInbox');
} catch (e) {
await resetRegistration();
void errorCallback(e);
throw e;
}
}
@ -92,10 +96,6 @@ export const CreateAccount = () => {
await signUp({
displayName,
recoveryPassword,
errorCallback: e => {
dispatch(setDisplayNameError(e.message || String(e)));
throw e;
},
});
dispatch(setAccountCreationStep(AccountCreation.Done));
@ -103,6 +103,7 @@ export const CreateAccount = () => {
window.log.debug(
`WIP: [onboarding] create account: creation failed! Error: ${e.message || e}`
);
dispatch(setDisplayNameError(e.message || String(e)));
dispatch(setAccountCreationStep(AccountCreation.DisplayName));
}
};

@ -32,19 +32,21 @@ import { SpacerLG, SpacerSM } from '../../basic/Text';
import { SessionIcon } from '../../icon';
import { SessionInput } from '../../inputs';
import { SessionProgressBar } from '../../loading';
import { RecoverDetails } from '../RegistrationStages';
import { OnboardDescription, OnboardHeading } from '../components';
import { BackButtonWithininContainer } from '../components/BackButton';
import { useRecoveryProgressEffect } from '../hooks';
import { displayNameIsValid, resetRegistration, sanitizeDisplayNameOrToast } from '../utils';
import { AccountDetails } from './CreateAccount';
type AccountRestoreDetails = AccountDetails & { dispatch: Dispatch };
/**
* Sign in/restore from seed.
* Ask for a display name, as we will drop incoming ConfigurationMessages if any are saved on the swarm.
* We will handle a ConfigurationMessage
*/
async function signInWithNewDisplayName(signInDetails: RecoverDetails, dispatch: Dispatch) {
const { displayName, recoveryPassword, errorCallback } = signInDetails;
async function signInWithNewDisplayName(args: AccountRestoreDetails) {
const { displayName, recoveryPassword, dispatch } = args;
try {
const validDisplayName = displayNameIsValid(displayName);
@ -62,7 +64,7 @@ async function signInWithNewDisplayName(signInDetails: RecoverDetails, dispatch:
);
} catch (e) {
await resetRegistration();
void errorCallback(e);
throw e;
}
}
@ -71,14 +73,13 @@ async function signInWithNewDisplayName(signInDetails: RecoverDetails, dispatch:
* If no ConfigurationMessage is received within ONBOARDING_RECOVERY_TIMEOUT, the user will be asked to enter a display name.
*/
async function signInAndFetchDisplayName(
signInDetails: RecoverDetails & {
args: AccountRestoreDetails & {
/** this is used to trigger the loading animation further down the registration pipeline */
loadingAnimationCallback: () => void;
},
abortSignal: AbortSignal,
dispatch: Dispatch
abortSignal: AbortSignal;
}
) {
const { recoveryPassword, loadingAnimationCallback } = signInDetails;
const { recoveryPassword, loadingAnimationCallback, dispatch, abortSignal } = args;
try {
await resetRegistration();
@ -141,30 +142,22 @@ export const RestoreAccount = () => {
`WIP: [onboarding] restore account: recoverAndFetchDisplayName() is starting recoveryPassword: ${recoveryPassword}`
);
dispatch(setProgress(0));
await signInAndFetchDisplayName(
{
recoveryPassword,
errorCallback: e => {
throw e;
},
loadingAnimationCallback: () => {
dispatch(setAccountRestorationStep(AccountRestoration.Loading));
},
await signInAndFetchDisplayName({
recoveryPassword,
loadingAnimationCallback: () => {
dispatch(setAccountRestorationStep(AccountRestoration.Loading));
},
abortController.signal,
dispatch
);
dispatch,
abortSignal: abortController.signal,
});
} catch (e) {
window.log.debug(
`WIP: [onboarding] restore account: restoration failed! Error: ${e.message || e}`
);
if (e instanceof NotFoundError || e instanceof TaskTimedOutError) {
if (e instanceof TaskTimedOutError) {
// abort fetching the display name from our swarm
window.log.debug(`WIP: [onboarding] restore account: aborting!`);
abortController.abort();
}
// abort the loading animation or display name polling if we get these errors. Now we enter a display name manually
abortController.abort();
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
return;
}
@ -189,21 +182,16 @@ export const RestoreAccount = () => {
window.log.debug(
`WIP: [onboarding] restore account: recoverAndEnterDisplayName() is starting recoveryPassword: ${recoveryPassword} displayName: ${displayName}`
);
await signInWithNewDisplayName(
{
displayName,
recoveryPassword,
errorCallback: e => {
dispatch(setDisplayNameError(e.message || String(e)));
throw e;
},
},
dispatch
);
await signInWithNewDisplayName({
displayName,
recoveryPassword,
dispatch,
});
} catch (e) {
window.log.debug(
`WIP: [onboarding] restore account: restoration with new display name failed! Error: ${e.message || e}`
);
dispatch(setDisplayNameError(e.message || String(e)));
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
}
};

@ -694,7 +694,7 @@ export class SwarmPolling {
// check if we just fetched the details from the config namespaces.
// If yes, merge them together and exclude them from the rest of the messages.
if (!resultsFromUserProfile?.length) {
throw new NotFoundError('resultsFromUserProfile is empty');
throw new NotFoundError('[pollOnceForOurDisplayName] resultsFromUserProfile is empty');
}
if (abortSignal?.aborted) {
@ -709,12 +709,16 @@ export class SwarmPolling {
const userConfigMessagesMerged = flatten(compact(userConfigMessages));
if (!userConfigMessagesMerged.length) {
throw new NotFoundError('after merging there are no user config messages');
throw new NotFoundError(
'[pollOnceForOurDisplayName] after merging there are no user config messages'
);
}
const displayName = await this.handleSharedConfigMessages(userConfigMessagesMerged, true);
if (isEmpty(displayName)) {
throw new NotFoundError('Got a config message from network but without a displayName...');
throw new NotFoundError(
'[pollOnceForOurDisplayName] Got a config message from network but without a displayName...'
);
}
// window.log.debug(`[pollOnceForOurDisplayName] displayName found ${displayName}`);

@ -10,7 +10,6 @@ import { SessionKeyPair } from '../receiver/keypairs';
import { getSwarmPollingInstance } from '../session/apis/snode_api';
import { mnDecode, mnEncode } from '../session/crypto/mnemonic';
import { getOurPubKeyStrFromCache } from '../session/utils/User';
import { NotFoundError } from '../session/utils/errors';
import { LibSessionUtil } from '../session/utils/libsession/libsession_utils';
import { actions as userActions } from '../state/ducks/user';
import { Registration } from './registration';
@ -85,15 +84,12 @@ export async function signInByLinkingDevice(
await saveRecoveryPhrase(mnemonic);
const pubKeyString = toHex(identityKeyPair.pubKey);
const displayName = await getSwarmPollingInstance().pollOnceForOurDisplayName(abortSignal);
if (isEmpty(pubKeyString)) {
throw new Error("We don't have a pubkey from the recovery password...");
}
if (isEmpty(displayName)) {
throw new NotFoundError('Got a config message from network but without a displayName...');
}
const displayName = await getSwarmPollingInstance().pollOnceForOurDisplayName(abortSignal);
// NOTE the registration is not yet finished until the configurationMessageReceived event has been processed
trigger(configurationMessageReceived, pubKeyString, displayName);

Loading…
Cancel
Save