Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase auth not work with PWA installed on Android desktop

I have built a SPA that works in the browser in that on load it provides an auth option, I click google signin and the firebase auth flow continues until I have an auth token etc.

I can then switch the PWA and use as normal. But if I then logout, I cannot log back in again with the App using Google Auth - whether I let the google signin popup run in the App or in Chrome, no confirmation ever gets abck to the App and indeed it seems to crash.

The issue has something to do with the loading of the additional tab for the google signin. According to the onscreen dialog Android asks me whether to open this tab in the PWA or in Chrome. Whichever option I pick, the flow does not complete (and because of the disconnection, I can't see anything useful in devtools).

The only flow that seems to work is to continue the login on chrome and, only when that has completed, switch to the App version. That's fine for me to write on StackOverflow but very complicated for my users.

How can I begin to debug this situation: - should it be possible to do firebase auth from a PWA; and/or - is there someway to delay the Android popup to add to home screen to til after the user has logged in on the browser?

Happy to share code, and this is the googlesignin function - it doesn't do anything because I wait for the onAuthState message in my code usually and that has all the information I need.

function signinGoogle() {
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives you a Google Access Token. You can use it to access the Google API.
        var token = result.credential.accessToken;
        // The signed-in user info.
        var user = result.user;
        // ...
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    });
}
like image 721
Simon H Avatar asked Nov 07 '22 16:11

Simon H


1 Answers

Per @jasan 's request, I did find a solution based on @bojeil 's comment

function signinGoogle(cb) {
    var provider = new firebase.auth.GoogleAuthProvider();
    // firebase.auth().signInWithPopup(provider).then(function(result) {
    firebase.auth().signInWithRedirect(provider).then(function(result) {
        console.log("Google signin successful")
        // This gives you a Google Access Token. You can use it to access the Google API.
        // var token = result.credential.accessToken;

        // Send user  to rest of program
        cb(token)
    })
    .catch(function(error) {
        logger(error);
    });
}
like image 167
Simon H Avatar answered Nov 14 '22 23:11

Simon H