From 355517ac530cc939871cc0d45a2894cd3a27df13 Mon Sep 17 00:00:00 2001 From: Miodec Date: Fri, 29 Jan 2021 20:34:10 +0000 Subject: [PATCH] fixed account page not loading properly, added sign in with google, added name check --- src/js/account.js | 90 ++++++++++++++++++++++++++++++++++++++++++++--- src/js/db.js | 4 +++ src/js/misc.js | 9 +++++ src/js/script.js | 3 +- static/index.html | 7 +++- 5 files changed, 107 insertions(+), 6 deletions(-) diff --git a/src/js/account.js b/src/js/account.js index efe2493ca..7d4cdbcd3 100644 --- a/src/js/account.js +++ b/src/js/account.js @@ -1,3 +1,7 @@ +import { db_updateName } from "./db"; + +var gmailProvider = new firebase.auth.GoogleAuthProvider(); + function showSignOutButton() { $(".signOut").removeClass("hidden").css("opacity", 1); } @@ -48,6 +52,40 @@ function signIn() { } } +async function signInWithGoogle() { + $(".pageLogin .preloader").removeClass("hidden"); + + if ($(".pageLogin .login #rememberMe input").prop("checked")) { + //remember me + await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL); + firebase + .auth() + .signInWithPopup(gmailProvider) + .then((result) => { + console.log(result); + }) + .catch((error) => { + Notifications.add(error.message, -1); + $(".pageLogin .preloader").addClass("hidden"); + }); + } else { + //dont remember + await firebase + .auth() + .setPersistence(firebase.auth.Auth.Persistence.SESSION); + firebase + .auth() + .signInWithPopup(gmailProvider) + .then((result) => { + console.log(result); + }) + .catch((error) => { + Notifications.add(error.message, -1); + $(".pageLogin .preloader").addClass("hidden"); + }); + } +} + let dontCheckUserName = false; function signUp() { @@ -270,10 +308,46 @@ firebase.auth().onAuthStateChanged(function (user) { function getAccountDataAndInit() { db_getUserSnapshot() - .then((e) => { - if (db_getSnapshot() === null) { + .then(async (e) => { + let snap = db_getSnapshot(); + if (snap === null) { throw "Missing db snapshot. Client likely could not connect to the backend."; } + let user = firebase.auth().currentUser; + if (snap.name === undefined) { + //verify username + if (Misc.isUsernameValid(user.displayName)) { + //valid, just update + snap.name = user.displayName; + db_setSnapshot(snap); + db_updateName(user.uid, user.displayName); + } else { + //invalid, get new + Notifications.add("Invalid name", 0); + let promptVal = null; + let cdnVal = undefined; + + while ( + promptVal === null || + cdnVal === undefined || + cdnVal.data.status < 0 + ) { + promptVal = prompt( + "Your name is either invalid or unavailable (you also need to do this if you used Google Sign Up). Please provide a new display name (cannot be longer than 14 characters, can only contain letters, numbers, underscores, dots and dashes):" + ); + cdnVal = await CloudFunctions.changeDisplayName({ + uid: user.uid, + name: promptVal, + }); + if (cdnVal.data.status === 1) { + Notifications.add("Name updated", 1); + location.reload(); + } else if (cdnVal.data.status < 0) { + Notifications.add(cdnVal.data.message, 0); + } + } + } + } if (!configChangedBeforeDb) { if (cookieConfig === null) { accountIconLoading(false); @@ -363,7 +437,10 @@ function getAccountDataAndInit() { // ) { // config.resultFilters.funbox = defaultAccountFilters.funbox; // } - if ($(".pageLogin").hasClass("active")) { + if ( + $(".pageLogin").hasClass("active") || + window.location.pathname === "/account" + ) { changePage("account"); } refreshThemeButtons(); @@ -2415,11 +2492,16 @@ $(".pageLogin .login input").keyup((e) => { } }); -$(".pageLogin .login .button").click((e) => { +$(".pageLogin .login .button.signIn").click((e) => { configChangedBeforeDb = false; signIn(); }); +$(".pageLogin .login .button.signInWithGoogle").click((e) => { + configChangedBeforeDb = false; + signInWithGoogle(); +}); + $(".signOut").click((e) => { signOut(); }); diff --git a/src/js/db.js b/src/js/db.js index 2f28d30a8..0e5fc9a27 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -5,6 +5,10 @@ db.settings({ experimentalForceLongPolling: true }); let dbSnapshot = null; +export function db_updateName(uid, name) { + db.collection(`users`).doc(uid).set({ name: name }, { merge: true }); +} + export function db_getSnapshot() { return dbSnapshot; } diff --git a/src/js/misc.js b/src/js/misc.js index 92a4cf5dd..ba9ecce52 100644 --- a/src/js/misc.js +++ b/src/js/misc.js @@ -566,3 +566,12 @@ export function cleanTypographySymbols(textToClean) { }; return textToClean.replace(/[“”’‘—,…«»–]/g, (char) => specials[char] || ""); } + +export function isUsernameValid(name) { + if (name === null || name === undefined || name === "") return false; + if (/miodec/.test(name.toLowerCase())) return false; + if (/bitly/.test(name.toLowerCase())) return false; + if (name.length > 14) return false; + if (/^\..*/.test(name.toLowerCase())) return false; + return /^[0-9a-zA-Z_.-]+$/.test(name); +} diff --git a/src/js/script.js b/src/js/script.js index 7521672e7..493507195 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -5690,9 +5690,10 @@ $(document).ready(() => { history.replaceState("/", null, "/"); } } else if (window.location.pathname === "/account") { - history.replaceState("/", null, "/"); + // history.replaceState("/", null, "/"); } else if (/challenge_.+/g.test(window.location.pathname)) { //do nothing + // } } else if (window.location.pathname !== "/") { let page = window.location.pathname.replace("/", ""); changePage(page); diff --git a/static/index.html b/static/index.html index d84600672..fb7bbd15b 100644 --- a/static/index.html +++ b/static/index.html @@ -3151,10 +3151,15 @@ Remember me -
+ +
or
+
+ + Google Sign In +