Allow tab continuation on Email confirmation
This commit is contained in:
parent
dd5056cfb3
commit
07d24c91d8
2 changed files with 44 additions and 11 deletions
|
@ -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);
|
||||||
|
|
|
@ -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'>
|
||||||
|
|
Loading…
Reference in a new issue