firebase / firebaseui-web

FirebaseUI is an open-source JavaScript library for Web that provides simple, customizable UI bindings on top of Firebase SDKs to eliminate boilerplate code and promote best practices.
https://firebase.google.com/
Apache License 2.0
4.56k stars 1.05k forks source link

Firebase google auth not working on Cordova/Android #203

Closed blukis closed 6 years ago

blukis commented 7 years ago

I can't get Firebase Auth (Google) to work on PhoneGap-build/Android - works great on PhoneGap/iOS, and directly on the web. (Disclosure - I don't know where to post this inquiry, please redirect me if there's a better place.)

For an example, and to help guide those stuck where I'm stuck, I followed this tutorial (https://firebase.google.com/docs/auth/web/cordova) and created a ~"bare minimum" Cordova app that builds at https://build.phonegap.com. (Maybe it could even be put up on github.com/firebase, as example code, to supplement the tutorial, once it's working?) The built app works great on iOS, but is broken on Android.

Expected behavior (seen on web/iOS):

Not-working behavior (seen on Android):

3-file cordova project attached. (.zip can be uploaded as-is to build.phonegap.com, to replicate above behavior in native apps) Thanks!

firebase-bdemo1.zip

bojeil-google commented 7 years ago

Sorry for the delay. I am swamped with stuff lately.

I didn't have the time to look at your code, but here are some tips for Cordova/Ionic apps in Android: In your config.xml file make sure you add: <preference name="AndroidLaunchMode" value="singleTask" />

After sign-in, are you getting redirected back to the app? If no, then perhaps you didn't register your android app with SHA-1 and SHA-256 cert in the Console. Also make sure you installed cordova-universal-links-plugin and that config.xml has the FDL domain and the AUTH_DOMAIN added. If yes, then I speculated the launchMode is the cause or perhaps the universal-link plugin is not installed.

blukis commented 7 years ago

Per your suggestion, I tried adding: <preference name="AndroidLaunchMode" value="singleTask" /> Also, since my original post, I had also tried: <preference name="AndroidLaunchMode" value="singleInstance" /> (per https://goo.gl/7nBVx4) but both (and also without either preference tags) all have the same behavior: It does successfully redirect to app, but app seems to startup again as though no useful response came back from authentication. (i.e. app opens again, but without any indication something went wrong -- like authentication never happened.)

I am building with build.phonegap.com - I don't know if this has anything to do with the issue. (This is the one deviation I made from the tutorial.)

Thank you for your time. I've been pulling my hair out over this, and I hope it might help other developers like me.

bojeil-google commented 7 years ago

I have only tested with Ionic/Cordova. I have not tested with build.phonegap.com. It may have something to do with that. I speculate the incoming link is not getting picked up. Try logging it when the app is relaunched:

universalLinks.subscribe(null, function (eventData) {
  // do some work
  console.log('Did launch application from the link: ' + eventData.url);
});
blukis commented 7 years ago

As you speculated, the universallinks event never seems to fire (before or after authentication round trip).

if (window.cordova) {
  AddDebug('universalLinks.subscribe()'); // CALLED OK.
  universalLinks.subscribe(null, function(eventData) {
    AddDebug('Did launch application from the link: ' + eventData.url); // NEVER CALLED.
  });
}

(On iOS, this also never fires (I think), yet authentication works OK -- presumably iOS doesn't need universallinks in order to work?)

bojeil-google commented 7 years ago

iOS doesn't use universal links. It uses custom scheme redirect (cordova-plugin-customurlscheme). I assume you set this up correctly:

<universal-links>
    <host name="DYNAMIC_LINK_DOMAIN" scheme="https" />
    <host name="AUTH_DOMAIN" scheme="https">
        <path url="/__/auth/callback"/>
    </host>
</universal-links>

Can you try testing this plugin separately (without Firebase in a basic test app) using build.phonegap.com with some universal link. It could be an issue with the plugin.

kiwi-josh commented 7 years ago

I am also experiencing exactly the same issue. (authentication works on web, but not in cordova ember build - never redirects back to the cordova app)

blukis commented 7 years ago

@wytlytningNZ, we might have slightly different issues. For mine, redirection back to the app happens OK, but no authentication event is triggered. As @bojeil-google helped narrow down, this is ultimately because the "cordova-universal-links-plugin" doesn't work on Android when building cordova app with PhoneGap Build [other case I opened for that plugin], resolution still TBD.

I've started learning how to build without PGB (locally with cordova). Initial tests seem to show authentication in Android works OK that way, and confirms it is in fact related to cordova-universal-links-plugin (in)compatibility with PhoneGap Build.

@wytlytningNZ - looks like @bojeil-google had some advice above for the case where redirection back to app doesn't happen. (didn't apply to me, but might apply to your case.?)

@bojeil-google - since PhoneGap Build is convenient for me and others [Getting local cordova running has been quite a learning curve for me the last week!], I wanted to inquire about the possibility of changing firebase to use custom scheme urls (already supported through "cordova-plugin-customurlscheme" plugin) instead of ("https") universal-links, for Android. This would make one fewer [problematic for some] plugin dependency for full iOS/Android/Web firebase authentication. (I assume this is a long shot, but wanted to ask. It was a surprise to hear iOS & Android worked differently in that regard.)

bojeil-google commented 7 years ago

My recommendation is to fix the issue with the plugin to support PhoneGap Build. The evidence is not conclusive yet that the issue is in the plugin. If you can test Android app links separately without using Firebase Auth and you get it to work, then the issue could be on our end. We would be able to fix that once that is established.

Unlike the App store, Android applications can be distributed without going through an app market store like the Google Play store. Any application can claim a custom scheme (that it doesn't own) and in some cases a developer has no means to take down such applications (unlike the Apple app store). https links are recommended for Android apps as custom scheme redirects are far less secure.

blukis commented 7 years ago

@bojeil-google - got it. Thanks for the explanation on https vs custom-scheme.

Possibly making progress on universal-links plugin PGB workaround, but still not quite there.

bojeil-google commented 7 years ago

Thanks, @blukis. If you find the correct way to configure the plugin to work for PGB, I'll make sure it's documented in our official docs for other PGB developers.

vishuhanda001 commented 6 years ago

the user does not come back in callback although it comes back when you are subscribed to check authstatus of user .

and hence you can login but there is a bug that when universal plugin browser gets closed why the user does not come in callback .

bojanbizjak commented 6 years ago

Is it available or will there be a solution to the problem (The redirection operation has been calncelled...) in the near future?
redirectcancellederr

bojeil-google commented 6 years ago

There was an issue we recently discovered and fixed in the core SDK and firebaseUI that is related to FirebaseUI in Cordova Android Apps, please update FirebaseUI-web to 2.6.0 and firebase to 4.9.1:

https://github.com/firebase/firebaseui-web/releases/tag/v2.6.0 https://firebase.google.com/support/release-notes/js#4.9.1

bradleyDean commented 6 years ago

I had a similar issue. Not sure if this helps, but since I installed plugin code with npm install, the plugins/android.json file's "installed_plugins" property was not updated. ...so it's possible to have the whitelist plugin code in your node_modules folder, but since it's not declared in plugins/android.json, cordova does not include it (or something... I don't claim to know the details of why this file matters).

Maybe check that file?

rtm commented 5 years ago

You may have seen that recently the cordova-universal-links-plugin was deprecated, and it was announced that it was not going to be supported or even bug-fixed anymore, and the comment thread was closed and locked. It seems that you need to come up with some definitive alternative, and stop recommending its use, as is done here in the Firebase docs.

bojeil-google commented 5 years ago

Thanks for the notice @rtm. We are updating our docs. You can use cordova-universal-links-plugin-fix instead. The core SDK also should be updated by now: https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/cordovahandler.js#L181

flamz3d commented 5 years ago

@blukis I've used your example to start my own firebase app with auth. I managed to get the example you attached working with cordova-universal-links-plugin-fix with your firebase app settings. However, I tried your project on my own firebase app and get the same behaviour you are describing: {user : null} no creds.

What I've done:

But after choosing the Google account in the auth flow, it redirects to my app without any creds.

Is there anything else you've setup in your firebase app that could explain my problem? When I switch the settings back to point to your project, everything works fine.

I build/compile and run locally, on-device.

thrinz commented 5 years ago

@flamz3d - would you be able to share your working code. I am struggling with the same issue.

50l3r commented 4 years ago

I have the same issue. I recieved user=null.

Any suggestions?

donovancarthew commented 4 years ago

I am having the same issue. For me, I am having this issue on iOS. Using an angular project. I am also getting {user : null} (no creds). Trying to sign in with Google. I followed the steps here: https://firebase.google.com/docs/auth/web/cordova

It happens only for new users who are signing in for the first time. And it only happens the first time I attempt to login. Once I have attempted the flow at least one, the second time it works (without asking me for a password). However, when I try to sign in with Facebook, it does not work at all.

satvik22 commented 4 years ago

I am having the same issue. For me, I am having this issue on iOS. Using an angular project. I am also getting {user : null} (no creds). Trying to sign in with Google. I followed the steps here: https://firebase.google.com/docs/auth/web/cordova

It happens only for new users who are signing in for the first time. And it only happens the first time I attempt to login. Once I have attempted the flow at least one, the second time it works (without asking me for a password). However, when I try to sign in with Facebook, it does not work at all.

Same problem as well! Its not working for me on android as well.

leblancmeneses commented 4 years ago

Like everyone else here iOS is working but not android. I am also using phone gap build so I can eventually hand the keys to my client.

The docs state the following is required: https://github.com/google/cordova-plugin-browsertab However, I am currently using cordova-plugin-safariviewcontroller due to: https://github.com/google/cordova-plugin-browsertab/issues/26

using


<script>
/* Check Plugins */
if ( window.cordova.plugins === undefined ) {
    window.cordova.plugins = {};
}

/* Browsertab */
window.cordova.plugins.browsertab = {};

/* Browsertab - Is Available */
window.cordova.plugins.browsertab.isAvailable = function( success ) {
    window.SafariViewController.isAvailable( success );
}

/* Browsertab - Open Url */
window.cordova.plugins.browsertab.openUrl = function( url ) {
    window.SafariViewController.show( { url: url } , function() {} , function() {} );
}

/* Browsertab - Close */
window.cordova.plugins.browsertab.close = function() {
    window.SafariViewController.hide();
}
</script>

I also forked cordova-universal-links-plugin to include https://github.com/nordnet/cordova-universal-links-plugin/pull/122 and a manifest fix https://github.com/nordnet/cordova-universal-links-plugin/issues/146 that caused npx phonegap cordova build android to fail locally. And allowed me to use the same name in package.json and config.xml - https://github.com/apache/cordova-docs/issues/1004.

main.ts:

if (typeof window.cordova !== 'undefined') {
  document.addEventListener('deviceready', () => {

    console.log('before universalLinks.subscribe()');
    window.universalLinks.subscribe(null, (eventData) => {
      console.log('Did launch application from the link: ' + eventData.url);
    });
    console.log('after universalLinks.subscribe()');

    onDeviceReadyBootstrap();
  }, false);
} else {
  onDeviceReadyBootstrap();
}

The before and after log message is logged but not the window.universalLinks.subscribe callback log message.

After invoking: signInWithRedirect the browser goes through normal oauth flow but when the flow completes and it sends me back to the app I'm never "really" logged in. 1) log message in subscribe block is never called 2) I sometimes see: 05-14 07:43:22.673 D/SystemWebChromeClient( 4646): file:///android_asset/www/main.4f2d9e34055435bc6bad.js: Line 1 : ERROR Error: Uncaught (in promise): Error: The redirect operation has been cancelled by the user before finalizing.

Creating a dynamic link in firebase console redirects me to the app but the log message in subscribe block is never called.

Is there a template that can be attached to https://firebase.google.com/docs/auth/web/cordova with a working setup in 2020?

Is there a docker image Google uses for cordova with correct version to produce a working apk? cordova version, android sdk version ect...

Thanks,

paridao commented 3 years ago

Hi @leblancmeneses , have you found a definitive solution? I'm stuck in the same nightmare. I'm getting the error: "The redirect operation has been cancelled by the user...". Anyone has found a solution in october 2020?

Than you very much