Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I refresh my page Firebase authData becomes null

var user = null;    

this.showLoginDialogGoogle  = function(){

        var ref = new Firebase("https://googlelogin1.firebaseio.com");
        ref.authWithOAuthPopup("google", function(error, authData) {
            if (error) {
                console.log("Login Failed!", error);
            } else {
                console.log("Authenticated successfully with payload:", authData);
                user = authData;
                This.goToPage('/profile');
                $rootScope.flag=true;
                $rootScope.$digest();
            }
        });
    };

I have authenticated the user with firebase google authentication. The problem is when I refresh my page, my session expires and authData becomes null. What can I do so that after refreshing the page my authData remains with the data it got at the time of authentication?

like image 634
Hammad Tariq Avatar asked Aug 11 '15 21:08

Hammad Tariq


1 Answers

You'll want to monitor authentication state:

// Register the callback to be fired every time auth state changes
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function authDataCallback(authData) {
  if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
  } else {
    console.log("User is logged out");
  }
});

Note that you're not using the AngularFire authentication wrappers. While your approach will authenticate the user, you will have to notify Angular of the updated scope yourself (see $timeout()). If you'd rather not do that yourself, look at AngularFire's auth wrappers, specifically $onAuth()

like image 118
Frank van Puffelen Avatar answered Oct 03 '22 07:10

Frank van Puffelen