Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
fix(auth): display warning message when upgrading using different ema…
Browse files Browse the repository at this point in the history
…il address
  • Loading branch information
rhahao committed Feb 19, 2023
1 parent 1a6e17c commit bc6af92
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/features/startup/EmailAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ const EmailAuth = () => {
const auth = await getAuth();
if (auth) {
const user = auth.currentUser;
const currentEmail = user.email;

if (currentEmail !== userTmpEmail) {
setAppMessage(t('oauthAccountUpgradeEmailMismatch'));
setAppSeverity('warning');
setAppSnackOpen(true);
return;
}

await apiRequestPasswordlesssLink(userTmpEmail, user.uid);
}

Expand Down
18 changes: 17 additions & 1 deletion src/features/startup/OAuthButtonBase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ const OAuthButtonBase = ({ buttonStyles, logo, text, provider, isEmail }) => {
try {
const auth = getAuth();
if (auth.currentUser) {
await linkWithPopup(auth.currentUser, provider);
const currentEmail = auth.currentUser.email;

const result = await linkWithPopup(auth.currentUser, provider);
const user = result.user;

const findNewProvider = user.providerData.find((tmp) => tmp.providerId === provider.providerId);
const newEmail = findNewProvider.email;

if (currentEmail !== newEmail) {
await unlink(auth.currentUser, provider.providerId);

setAppMessage(t('oauthAccountUpgradeEmailMismatch'));
setAppSeverity('warning');
setAppSnackOpen(true);
return;
}

await unlink(auth.currentUser, 'password');
}

Expand Down
14 changes: 12 additions & 2 deletions src/features/userAutoLogin/UserAutoLogin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
apiHostState,
isAppLoadState,
isOAuthAccountUpgradeState,
isOnlineState,
rootModalOpenState,
userIDState,
Expand All @@ -35,6 +36,7 @@ const UserAutoLogin = () => {
const apiHost = useRecoilValue(apiHostState);
const visitorID = useRecoilValue(visitorIDState);
const isAppLoad = useRecoilValue(isAppLoadState);
const isOAuthAccountUpgrade = useRecoilValue(isOAuthAccountUpgradeState);

const handleDisapproved = useCallback(async () => {
setModalOpen(true);
Expand Down Expand Up @@ -114,13 +116,21 @@ const UserAutoLogin = () => {
]);

useEffect(() => {
if (!isAppLoad && isOnline && isAuthenticated) {
if (!isOAuthAccountUpgrade && !isAppLoad && isOnline && isAuthenticated) {
checkLogin();
} else {
setCongAccountConnected(false);
setIsAdminCong(false);
}
}, [isAppLoad, isAuthenticated, checkLogin, isOnline, setCongAccountConnected, setIsAdminCong]);
}, [
isAppLoad,
isAuthenticated,
isOAuthAccountUpgrade,
checkLogin,
isOnline,
setCongAccountConnected,
setIsAdminCong,
]);

return <></>;
};
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
"mfaTokenInvalidExpired": "This code is invalid or has expired. Get a new code from the authenticator app and try again",
"oauthAccountExistsWithDifferentCredential": "Your account is already linked to another service. Please login using that service instead.",
"oauthAccountUpgradeError": "An error occured while upgrading your account. Please try again later.",
"oauthAccountUpgradeEmailMismatch": "Account upgrade failed: Please login using your current email address when opening CPE.",
"oauthAccountUpgradeComplete": "Your account has been upgraded. This page will now reload.",
"oauthAccountUpgradeEmailComplete": "Your account has been upgraded. A message has been sent to your email address to complete your authentication.",
"logoutOnlineAccount": "Logout from online account",
Expand Down

0 comments on commit bc6af92

Please sign in to comment.