Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - Firebase Auth login state persistence is not working

On an ionic/AngularJS/Cordova IOS app.

I'm trying to set a persistence login using mail/password (followed Firebase documentation : https://firebase.google.com/docs/auth/web/auth-state-persistence). But I can't figured out what is wrong with my code. My app is starting on the login page, when a user is clicking on the "Connexion button", it's trigger the login function.

The login is working, but when I'm closing the app (without logging out), restarting it, I'm still on the login page. The login is not persistent.

So I guess, one problem, might be the persistency call not well placed...Is it ? According to this post : Firebase 3.0 session persistance it could have someting related to firebase.auth().onAuthStateChanged(function(user), but I don't know...

// EMAIL CONNEXION TRIGGERED WHEN CONNEXION BUTTON IS HIT

$scope.loginEmail = function($email, $password){
  
  firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(function() {
    var alertPopup;
    
    function signInSuccess(response) {
  
$state.go("menu.VN");
    }

    function signInError(response) {
    
        var errorCode = null;
        errorCode = response.code;
        if ($email === ""){
             alertPopup = $ionicPopup.alert({
                    title: 'Something wrong...',
                    cssClass: 'pop',
                    template: '<div class="center-form">Need an email address...</div>'
                });   
        }
}

return firebase.auth().signInWithEmailAndPassword($email, $password)
  .then(signInSuccess)
  .catch(signInError);

     })
     .catch(function(error) {
    // Handle persistence Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });

};
<label>
          <span>Email</span>
          <input type="email" placeholder="" ng-model="data.email">
</label>

<label>
          <span>Password</span>
          <input type="password" placeholder="" ng-model="data.password">
</label>
    
<button ng-click="loginEmail(data.email, data.password )">Connexion</button>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...