From ab5ecb195adae1e54883a3460b311c9e2b44bde9 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 19 Jul 2021 19:05:24 +0100 Subject: [PATCH] updated danger zone popups when using google sign in --- src/js/account-controller.js | 2 +- src/js/simple-popups.js | 112 ++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 43 deletions(-) diff --git a/src/js/account-controller.js b/src/js/account-controller.js index 76c6c57fa..36b4dc78b 100644 --- a/src/js/account-controller.js +++ b/src/js/account-controller.js @@ -14,7 +14,7 @@ import * as DB from "./db"; import * as TestLogic from "./test-logic"; import * as UI from "./ui"; -const gmailProvider = new firebase.auth.GoogleAuthProvider(); +export const gmailProvider = new firebase.auth.GoogleAuthProvider(); const githubProvider = new firebase.auth.GithubAuthProvider(); export function signIn() { diff --git a/src/js/simple-popups.js b/src/js/simple-popups.js index a74f24cd0..5256d3e05 100644 --- a/src/js/simple-popups.js +++ b/src/js/simple-popups.js @@ -49,7 +49,12 @@ class SimplePopup { this.initInputs(); - el.find(".button").text(this.buttonText); + if (!this.buttonText) { + el.find(".button").remove(); + } else { + el.find(".button").text(this.buttonText); + } + // } } @@ -160,51 +165,55 @@ list.updateEmail = new SimplePopup( ], "", "Update", - (pass, email, emailConfirm) => { + async (password, email, emailConfirm) => { try { const user = firebase.auth().currentUser; - const credential = firebase.auth.EmailAuthProvider.credential( - user.email, - pass - ); if (email !== emailConfirm) { Notifications.add("Emails don't match", 0); return; } + if (user.providerData[0].providerId === "password") { + const credential = firebase.auth.EmailAuthProvider.credential( + user.email, + password + ); + await user.reauthenticateWithCredential(credential); + } Loader.show(); - user - .reauthenticateWithCredential(credential) - .then(() => { - CloudFunctions.updateEmail({ - uid: user.uid, - previousEmail: user.email, - newEmail: email, - }).then((data) => { - Loader.hide(); - if (data.data.resultCode === 1) { - Notifications.add("Email updated", 0); - setTimeout(() => { - AccountController.signOut(); - }, 1000); - } else if (data.data.resultCode === -1) { - Notifications.add("Current email doesn't match", 0); - } else { - Notifications.add( - "Something went wrong: " + JSON.stringify(data.data), - -1 - ); - } - }); - }) - .catch((e) => { - Loader.hide(); - Notifications.add("Incorrect current password", -1); - }); + CloudFunctions.updateEmail({ + uid: user.uid, + previousEmail: user.email, + newEmail: email, + }).then((data) => { + Loader.hide(); + if (data.data.resultCode === 1) { + Notifications.add("Email updated", 0); + setTimeout(() => { + AccountController.signOut(); + }, 1000); + } else if (data.data.resultCode === -1) { + Notifications.add("Current email doesn't match", 0); + } else { + Notifications.add( + "Something went wrong: " + JSON.stringify(data.data), + -1 + ); + } + }); } catch (e) { Notifications.add("Something went wrong: " + e, -1); } }, - () => {} + () => { + const user = firebase.auth().currentUser; + if (user.providerData[0].providerId === "google.com") { + eval(`this.inputs = []`); + eval(`this.buttonText = undefined`); + eval( + `this.text = "You can't change your email when using Google Authentication";` + ); + } + } ); list.updatePassword = new SimplePopup( @@ -251,7 +260,16 @@ list.updatePassword = new SimplePopup( Notifications.add(e, -1); } }, - () => {} + () => { + const user = firebase.auth().currentUser; + if (user.providerData[0].providerId === "google.com") { + eval(`this.inputs = []`); + eval(`this.buttonText = undefined`); + eval( + `this.text = "You can't change your password when using Google Authentication";` + ); + } + } ); list.clearTagPb = new SimplePopup( @@ -327,12 +345,16 @@ list.resetPersonalBests = new SimplePopup( async (password) => { try { const user = firebase.auth().currentUser; - const credential = firebase.auth.EmailAuthProvider.credential( - user.email, - password - ); + if (user.providerData[0].providerId === "password") { + const credential = firebase.auth.EmailAuthProvider.credential( + user.email, + password + ); + await user.reauthenticateWithCredential(credential); + } else if (user.providerData[0].providerId === "google.com") { + await user.reauthenticateWithPopup(AccountController.gmailProvider); + } Loader.show(); - await user.reauthenticateWithCredential(credential); let resetResult = await CloudFunctions.resetPersonalBests({ uid: firebase.auth().currentUser.uid, }); @@ -353,7 +375,13 @@ list.resetPersonalBests = new SimplePopup( Notifications.add(e, -1); } }, - () => {} + () => { + const user = firebase.auth().currentUser; + if (user.providerData[0].providerId === "google.com") { + eval(`this.inputs = []`); + eval(`this.buttonText = "Reauthenticate to reset"`); + } + } ); list.resetSettings = new SimplePopup(