Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: firebase auth issues on ios #2835

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/app/shared/services/auth/providers/firebase.auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable, Injector } from "@angular/core";
import { FirebaseAuthentication, User } from "@capacitor-firebase/authentication";
import { getAuth } from "firebase/auth";
import { getAuth, initializeAuth, indexedDBLocalPersistence, Auth } from "firebase/auth";
import { FirebaseService } from "../../firebase/firebase.service";
import { AuthProviderBase } from "./base.auth";
import { IAuthUser } from "../types";
import { FirebaseApp } from "firebase/app";
import { Capacitor } from "@capacitor/core";

/** LocalStorage field used to store temporary auth profile data */
const AUTH_METADATA_FIELD = "firebase_auth_openid_profile";
Expand All @@ -14,12 +16,15 @@ export type FirebaseAuthUser = User;
providedIn: "root",
})
export class FirebaseAuthProvider extends AuthProviderBase {
private auth: Auth;

public override async initialise(injector: Injector) {
const firebaseService = injector.get(FirebaseService);
// TODO - is service required here?
if (!firebaseService.app) {
throw new Error("[Firebase Auth] app not configured");
}
this.initialiseAuth(firebaseService.app);
await this.handleAutomatedLogin();
}

Expand All @@ -43,6 +48,22 @@ export class FirebaseAuthProvider extends AuthProviderBase {
return this.authUser();
}

/**
* Configure Firebase Auth to use indexedDB for persistence on native platforms.
* Default mechanism (cookies?) may not be available from capacitor://localhost domains (e.g. on ios)
* See https://stackoverflow.com/a/74904270
* and https://firebase.google.com/docs/auth/web/custom-dependencies#platform-specific_considerations
*/
private initialiseAuth(app: FirebaseApp) {
if (Capacitor.isNativePlatform()) {
this.auth = initializeAuth(app, {
persistence: indexedDBLocalPersistence,
});
} else {
this.auth = getAuth();
}
}

private setAuthUser(user: User, profile: Partial<IAuthUser>) {
const authUser: IAuthUser = {
...profile,
Expand All @@ -59,7 +80,7 @@ export class FirebaseAuthProvider extends AuthProviderBase {
private async handleAutomatedLogin() {
// use firebase authStateReady to ensure any previously logged in user is available
// and update the auth user with loaded profile
await getAuth().authStateReady();
await this.auth.authStateReady();
const { user } = await FirebaseAuthentication.getCurrentUser();
if (user) {
const storedProfile = localStorage.getItem(AUTH_METADATA_FIELD);
Expand Down
Loading