Allow tab continuation on Email confirmation

tajwal
Chewbacca 2022-10-24 09:05:17 -04:00
rodzic dd5056cfb3
commit 07d24c91d8
2 zmienionych plików z 44 dodań i 11 usunięć

Wyświetl plik

@ -32,13 +32,14 @@ export type Challenge = 'age' | 'sms' | 'email'
type Challenges = { type Challenges = {
email?: 0 | 1, email?: 0 | 1,
sms?: number, sms?: 0 | 1,
age?: number, age?: 0 | 1,
} }
type Verification = { type Verification = {
token?: string, token?: string,
challenges?: Challenges, challenges?: Challenges,
challengeTypes?: Array<'age' | 'sms' | 'email'>
}; };
/** /**
@ -83,6 +84,18 @@ const fetchStoredChallenges = () => {
} }
}; };
/**
* Fetch and return the state of the verification challenge types.
*/
const fetchStoredChallengeTypes = () => {
try {
const verification: Verification | null = fetchStoredVerification();
return verification!.challengeTypes;
} catch {
return null;
}
};
/** /**
* Update the verification object in local storage. * Update the verification object in local storage.
* *
@ -131,7 +144,10 @@ function saveChallenges(challenges: Array<'age' | 'sms' | 'email'>) {
} }
} }
updateStorage({ challenges: currentChallenges }); updateStorage({
challenges: currentChallenges,
challengeTypes: challenges,
});
} }
/** /**
@ -267,13 +283,29 @@ const confirmEmailVerification = (emailToken: string) =>
return api(getState).post('/api/v1/pepe/verify_email/confirm', { token: emailToken }, { return api(getState).post('/api/v1/pepe/verify_email/confirm', { token: emailToken }, {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
}) })
.then(() => { .then((response) => {
finishChallenge(EMAIL); updateStorageFromEmailConfirmation(dispatch, response.data.token);
dispatchNextChallenge(dispatch);
}) })
.finally(() => dispatch({ type: SET_LOADING, value: false })); .finally(() => dispatch({ type: SET_LOADING, value: false }));
}; };
const updateStorageFromEmailConfirmation = (dispatch: AppDispatch, token: string) => {
const challengeTypes = fetchStoredChallengeTypes();
if (!challengeTypes) {
return;
}
const indexOfEmail = challengeTypes.indexOf('email');
const challenges: Challenges = {};
challengeTypes?.forEach((challengeType, idx) => {
const value = idx <= indexOfEmail ? 1 : 0;
challenges[challengeType] = value;
});
updateStorage({ token, challengeTypes, challenges });
dispatchNextChallenge(dispatch);
};
const postEmailVerification = () => const postEmailVerification = () =>
(dispatch: AppDispatch) => { (dispatch: AppDispatch) => {
finishChallenge(EMAIL); finishChallenge(EMAIL);

Wyświetl plik

@ -40,11 +40,12 @@ const Success = () => {
const history = useHistory(); const history = useHistory();
const currentChallenge = useAppSelector((state) => state.verification.currentChallenge as ChallengeTypes); const currentChallenge = useAppSelector((state) => state.verification.currentChallenge as ChallengeTypes);
// Bypass the user straight to the next step. React.useEffect(() => {
if (currentChallenge === ChallengeTypes.SMS) { // Bypass the user straight to the next step.
history.push('/verify'); if (currentChallenge === ChallengeTypes.SMS) {
return null; history.push('/verify');
} }
}, [currentChallenge]);
return ( return (
<Stack space={4} alignItems='center'> <Stack space={4} alignItems='center'>