Firebase Auth登录状态持久性不起作用 [英] Firebase Auth login state persistence is not working

查看:65
本文介绍了Firebase Auth登录状态持久性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ionic/AngularJS/Cordova IOS应用上.

On an ionic/AngularJS/Cordova IOS app.

我正在尝试使用邮件/密码设置持久性登录(紧随Firebase documentation: https://firebase.google.com/docs/auth/web/auth-state-persistence ).但是我无法弄清楚我的代码出了什么问题.我的应用程序是在登录页面上启动的,当用户单击"Connexion按钮"时,会触发登录功能.

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.

所以我想,一个问题可能是持久性调用的位置不正确...对吗?根据这篇文章: Firebase 3.0会话持久性,它可能与firebase.auth().onAuthStateChanged(function(user)有关,但我不知道...

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>

推荐答案

要保持状态,应使用firebase.auth.Auth.Persistence.LOCAL而不是firebase.auth.Auth.Persistence.SESSION.

To persist your state, you should use firebase.auth.Auth.Persistence.LOCAL and not firebase.auth.Auth.Persistence.SESSION.

Cordova iOS应用中的localStorage存在易变的已知问题. Firebase Auth过去依赖于localStorage,但是最新版本使用更可靠的indexedDB来保留用户状态.如果此功能不可用,它会退回到localStorage.在这种情况下,您可以使用用于indexedDB的Microsoft插件.

There are known issues with localStorage in Cordova iOS apps being volatile. Firebase Auth used to rely on localStorage but the latest version uses more reliable indexedDB to persist the user state. When this is not available, it falls back to localStorage. In that case, you could use this Microsoft plugin for indexedDB.

tldr;迁移到最新版本的Firebase Auth,并在indexedDB不可用时使用Cordova indexedDB插件.

tldr; migrate to the latest version of Firebase Auth and use the Cordova indexedDB plugin when indexedDB is not available.

这篇关于Firebase Auth登录状态持久性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆