From ffee8c997135e05f65673e6645f96726977bd649 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Thu, 25 Jul 2024 12:08:38 +0200 Subject: [PATCH] wip --- .../src/ts/controllers/account-controller.ts | 33 ++++++++++++++++--- frontend/src/ts/firebase.ts | 6 ++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/frontend/src/ts/controllers/account-controller.ts b/frontend/src/ts/controllers/account-controller.ts index 6f465784e..74134b95c 100644 --- a/frontend/src/ts/controllers/account-controller.ts +++ b/frontend/src/ts/controllers/account-controller.ts @@ -34,6 +34,10 @@ import { User as UserType, Unsubscribe, AuthProvider, + signInWithRedirect, + getAuth, + getRedirectResult, + UserCredential, } from "firebase/auth"; import { Auth, getAuthenticatedUser, isAuthenticated } from "../firebase"; import { dispatch as dispatchSignUpEvent } from "../observables/google-sign-up-event"; @@ -229,9 +233,17 @@ async function readyFunction( authInitialisedAndConnected: boolean, user: UserType | null ): Promise { + console.log("###", user); const search = window.location.search; const hash = window.location.hash; console.debug(`account controller ready`); + + //check for redirect + + await getRedirectResult(getAuth()).then((result) => { + console.log("#### redirect ", result); + }); + if (authInitialisedAndConnected) { console.debug(`auth state changed, user ${user ? true : false}`); console.debug(user); @@ -334,7 +346,10 @@ async function signIn(): Promise { }); } -async function signInWithProvider(provider: AuthProvider): Promise { +async function signInWithProvider( + provider: AuthProvider, + usePopup = false +): Promise { if (Auth === undefined) { Notifications.add("Authentication uninitialized", -1, { duration: 3, @@ -348,6 +363,8 @@ async function signInWithProvider(provider: AuthProvider): Promise { return; } + // @ts-expect-error + provider.addScope("https://www.googleapis.com/auth/plus.login"); LoginPage.showPreloader(); LoginPage.disableInputs(); LoginPage.disableSignUpButton(); @@ -359,12 +376,18 @@ async function signInWithProvider(provider: AuthProvider): Promise { : browserSessionPersistence; await setPersistence(Auth, persistence); + + if (!usePopup) { + await signInWithRedirect(Auth, provider); + return; + } + signInWithPopup(Auth, provider) - .then(async (signedInUser) => { - if (getAdditionalUserInfo(signedInUser)?.isNewUser) { - dispatchSignUpEvent(signedInUser, true); + .then(async (credentials: UserCredential) => { + if (getAdditionalUserInfo(credentials)?.isNewUser) { + dispatchSignUpEvent(credentials, true); } else { - await loadUser(signedInUser.user); + await loadUser(credentials.user); } }) .catch((error) => { diff --git a/frontend/src/ts/firebase.ts b/frontend/src/ts/firebase.ts index 4f4912dd9..ccdee8c85 100644 --- a/frontend/src/ts/firebase.ts +++ b/frontend/src/ts/firebase.ts @@ -1,12 +1,9 @@ // Import the functions you need from the SDKs you need import { FirebaseApp, initializeApp } from "firebase/app"; -import { getAuth, Auth as AuthType, User } from "firebase/auth"; -// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -//@ts-ignore -// eslint-disable-next-line import/no-unresolved import { firebaseConfig } from "./constants/firebase-config"; import * as Notifications from "./elements/notifications"; import { createErrorMessage, isDevEnvironment } from "./utils/misc"; +import { getAuth, User, Auth as AuthType } from "firebase/auth"; // Initialize Firebase export let app: FirebaseApp | undefined; @@ -28,6 +25,7 @@ export function getAuthenticatedUser(): User { try { app = initializeApp(firebaseConfig); Auth = getAuth(app); + console.log("### init ", Auth.currentUser); } catch (e) { app = undefined; Auth = undefined;