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

fix: firebase auth issues on ios #2835

wants to merge 1 commit into from

Conversation

jfmcquade
Copy link
Collaborator

@jfmcquade jfmcquade commented Mar 6, 2025

PR Checklist

  • PR title descriptive (can be used in release notes)

Description

Fixes an issue where the Auth service with Firebase as the provider would not initialise correctly on ios. This manifested as the issues logged in this mattermost thread.

Testing

@esmeetewinkel to confirm login flows are working:

Dev notes

These issues proved difficult to debug, in part because initially I could only replicate them in appetize and not via my local emulator. This is explained by the fact that the issue is related specifically to serving the app from a capacitor://localhost domain, and I was testing locally by serving from my own IP address via yarn ng serve --configuration=external. See error log below.

Cross-origin redirection to http://developers.google.com/ denied by Cross-Origin Resource Sharing policy: Origin capacitor://localhost is not allowed by Access-Control-Allow-Origin. Status code: 301

Also, as the login functionality was still working via the dedicated google sign in button component, it wasn't immediately obvious that the auth service wasn't successfully initialising. The issue was with the FirebaseAuthentication.getCurrentUser() method specifically, run as part of the service init, which wasn't resolving and so blocked the full init whilst signInWithGoogle() could still be called successfully. The issue seems to be in how Firebase Auth stores its persisting data. This PR configures it to use indexedDB when on native platforms, in accordance with this answer and these docs. Using this configuration on web does not seem to work, so the platform check is required.

Git Issues

Closes #

Screenshots/Videos

@github-actions github-actions bot added the fix label Mar 6, 2025
@jfmcquade jfmcquade added test - appetize Build and deploy android apk to appetize.io and removed fix labels Mar 6, 2025
@jfmcquade jfmcquade added the test - preview Create a preview deployment of the PR label Mar 6, 2025
Copy link

github-actions bot commented Mar 6, 2025

Visit the preview URL for this PR (updated for commit 1dbc478):

https://plh-teens-app1--pr2835-fix-ios-auth-8zasq53u.web.app

(expires Thu, 20 Mar 2025 15:32:00 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: e4c0bab6b08dd290fbf002fd6e07987fa4b5fce1

Copy link
Collaborator

@esmeetewinkel esmeetewinkel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional test passed on iOS Appetize, Android native device and web preview

image

For future reference, I pasted the two screen videos of the issues this addresses in the mattermost chat referenced in the issue (the two screen videos that sit in the chat so far were addressed by authoring changes, I believe)

Copy link
Member

@chrismclarke chrismclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super interesting issue, thanks for the deep-dive on it @jfmcquade

Looking at the links you've shared and a quick google leads me to agree with your assessment - the getAuth() method assumes it is in a browser environment uses methods incompatible with capacitor.

I don't actually think it's an issue with indexeddb vs localstorage, but more how getAuth includes the browserPopupRedirectResolver which will not work when called. Setting the indexedDB persistence also enforces the cordovaPopupRedirectResolver instead
firebase/firebase-js-sdk#5019
https://firebase.google.com/docs/reference/js/auth.dependencies.md

There is part of me wondering whether all of this has been fixed in more recent updates for firebase and/or capacitor-firebase, as essentially the automatic behaviour used to check for URLs starting ionic:// which broke when capacitor updated to capacitor:// URLs way back in Capacitor 3 (discussed a bit in this thread

https://github.com/firebase/firebase-js-sdk/issues/5019

However I still think the fix you have provided makes a lot of sense and also reflects the guidance in the current capacitor-firebase docs

https://github.com/capawesome-team/capacitor-firebase/blob/main/packages/authentication/docs/firebase-js-sdk.md

I'm assuming post-merge any existing android users will be forced to log back in (could be worth checking), although I don't think that should have any real impact except perhaps if being prompted to restore profile which would depend a bit on deployment. It might still be good to quickly test following merge though @esmeetewinkel @jfmcquade

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test - appetize Build and deploy android apk to appetize.io test - preview Create a preview deployment of the PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants