From e584acfe3068a475446b6fdba7ef312832ecc4cf Mon Sep 17 00:00:00 2001 From: Miodec Date: Tue, 27 Sep 2022 22:12:41 +0200 Subject: [PATCH] added ready function if auth is undefined blocking functions if auth is undefined --- .../src/ts/controllers/account-controller.ts | 134 ++++++++++++++---- 1 file changed, 103 insertions(+), 31 deletions(-) diff --git a/frontend/src/ts/controllers/account-controller.ts b/frontend/src/ts/controllers/account-controller.ts index 42a3ebffb..5f738440c 100644 --- a/frontend/src/ts/controllers/account-controller.ts +++ b/frontend/src/ts/controllers/account-controller.ts @@ -38,6 +38,7 @@ import { getAdditionalUserInfo, sendPasswordResetEmail, User as UserType, + Unsubscribe, } from "firebase/auth"; import { Auth } from "../firebase"; import { defaultSnap } from "../constants/default-snapshot"; @@ -53,6 +54,10 @@ export const gmailProvider = new GoogleAuthProvider(); let canCall = true; export function sendVerificationEmail(): void { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } Loader.show(); const user = Auth.currentUser; if (user === null) return; @@ -274,47 +279,84 @@ export async function loadUser(user: UserType): Promise { } } -const authListener = Auth.onAuthStateChanged(async function (user) { - // await UpdateConfig.loadPromise; - const search = window.location.search; - const hash = window.location.hash; - console.log(`auth state changed, user ${user ? true : false}`); - if (user) { - $("#top .signInOut .icon").html( - `` - ); - await loadUser(user); - } else { +let authListener: Unsubscribe; + +// eslint-disable-next-line no-constant-condition +if (Auth) { + authListener = Auth?.onAuthStateChanged(async function (user) { + // await UpdateConfig.loadPromise; + const search = window.location.search; + const hash = window.location.hash; + console.log(`auth state changed, user ${user ? true : false}`); + if (user) { + $("#top .signInOut .icon").html( + `` + ); + await loadUser(user); + } else { + $("#top .signInOut .icon").html( + `` + ); + if (window.location.pathname == "/account") { + window.history.replaceState("", "", "/login"); + } + PageTransition.set(false); + } + if (!user) { + navigate(); + } + + URLHandler.loadCustomThemeFromUrl(search); + URLHandler.loadTestSettingsFromUrl(search); + URLHandler.linkDiscord(hash); + + if (/challenge_.+/g.test(window.location.pathname)) { + Notifications.add( + "Challenge links temporarily disabled. Please use the command line to load the challenge manually", + 0, + 7 + ); + return; + // Notifications.add("Loading challenge", 0); + // let challengeName = window.location.pathname.split("_")[1]; + // setTimeout(() => { + // ChallengeController.setup(challengeName); + // }, 1000); + } + }); +} else { + $("document").ready(async () => { + // await UpdateConfig.loadPromise; + const search = window.location.search; + const hash = window.location.hash; $("#top .signInOut .icon").html(``); if (window.location.pathname == "/account") { window.history.replaceState("", "", "/login"); } PageTransition.set(false); - } - if (!user) { navigate(); - } - URLHandler.loadCustomThemeFromUrl(search); - URLHandler.loadTestSettingsFromUrl(search); - URLHandler.linkDiscord(hash); + URLHandler.loadCustomThemeFromUrl(search); + URLHandler.loadTestSettingsFromUrl(search); + URLHandler.linkDiscord(hash); - if (/challenge_.+/g.test(window.location.pathname)) { - Notifications.add( - "Challenge links temporarily disabled. Please use the command line to load the challenge manually", - 0, - 7 - ); - return; - // Notifications.add("Loading challenge", 0); - // let challengeName = window.location.pathname.split("_")[1]; - // setTimeout(() => { - // ChallengeController.setup(challengeName); - // }, 1000); - } -}); + if (/challenge_.+/g.test(window.location.pathname)) { + Notifications.add( + "Challenge links temporarily disabled. Please use the command line to load the challenge manually", + 0, + 7 + ); + return; + } + }); +} export function signIn(): void { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } + UpdateConfig.setChangedBeforeDb(false); authListener(); LoginPage.showPreloader(); @@ -329,6 +371,7 @@ export function signIn(): void { : browserSessionPersistence; setPersistence(Auth, persistence).then(async function () { + if (Auth === undefined) return; //todo convert this function to async and remove this line return signInWithEmailAndPassword(Auth, email, password) .then(async (e) => { await loadUser(e.user); @@ -353,6 +396,10 @@ export function signIn(): void { } export async function forgotPassword(email: any): Promise { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } if (!canCall) { return Notifications.add( "Please wait before requesting another password reset link", @@ -378,6 +425,11 @@ export async function forgotPassword(email: any): Promise { } export async function signInWithGoogle(): Promise { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } + UpdateConfig.setChangedBeforeDb(false); LoginPage.showPreloader(); LoginPage.disableInputs(); @@ -418,6 +470,10 @@ export async function signInWithGoogle(): Promise { } export async function addGoogleAuth(): Promise { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } Loader.show(); if (Auth.currentUser === null) return; linkWithPopup(Auth.currentUser, gmailProvider) @@ -439,6 +495,10 @@ export async function addPasswordAuth( email: string, password: string ): Promise { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } Loader.show(); const user = Auth.currentUser; if (user === null) return; @@ -471,6 +531,10 @@ export async function addPasswordAuth( } export function signOut(): void { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } if (!Auth.currentUser) return; Auth.signOut() .then(function () { @@ -493,6 +557,10 @@ export function signOut(): void { } async function signUp(): Promise { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } RegisterCaptchaPopup.show(); const captcha = await RegisterCaptchaPopup.promise; if (!captcha) { @@ -664,6 +732,10 @@ $(".pageLogin .login .button.signInWithGoogle").on("click", () => { // }); $("#top .signInOut").on("click", () => { + if (Auth === undefined) { + Notifications.add("Offline mode", -1, 3); + return; + } if (Auth.currentUser) { signOut(); } else {