added ready function if auth is undefined

blocking functions if auth is undefined
This commit is contained in:
Miodec 2022-09-27 22:12:41 +02:00
parent 4582c3bdad
commit e584acfe30

View file

@ -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<void> {
}
}
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(
`<i class="fas fa-fw fa-sign-out-alt"></i>`
);
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(
`<i class="fas fa-fw fa-sign-out-alt"></i>`
);
await loadUser(user);
} else {
$("#top .signInOut .icon").html(
`<i class="fas fa-fw fa-sign-in-alt"></i>`
);
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(`<i class="fas fa-fw fa-sign-in-alt"></i>`);
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<void> {
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<void> {
}
export async function signInWithGoogle(): Promise<void> {
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<void> {
}
export async function addGoogleAuth(): Promise<void> {
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<void> {
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<void> {
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 {