首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无论如何,如何保持用户登录到Firebase?

无论如何,如何保持用户登录到Firebase?
EN

Stack Overflow用户
提问于 2017-03-28 15:14:00
回答 2查看 2.3K关注 0票数 2

我正在开发一个在Ionic框架下的应用程序,我也在使用Firebase。

现在,在几个小时后,或者由于应用程序崩溃或重新启动设备,身份验证就会丢失。

怎么能让我的用户总是登录,不管发生什么? (比如Facebook )

下面是来自页面 login.html的登录控制器:

代码语言:javascript
复制
.controller('loginController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
  // Perform the login action when the user submits the login form
  $scope.doLogin = function(userLogin) {
        if($document[0].getElementById("user_name").value != "" && $document[0].getElementById("user_pass").value != ""){
        firebase.auth().signInWithEmailAndPassword(userLogin.username, userLogin.password).then(function() {
          var userId = firebase.auth().currentUser.uid;
          firebase.database().ref('accounts/' + userId + '/currentBusiness/').update({
            name: "No current business arround",
            description: "Seems there's nothing arround...",

            })
                      $state.go("tab.favorites");

        }, function(error) {
          // An error happened.
          var errorCode = error.code;
          var errorMessage = error.message;

          if (errorCode === 'auth/invalid-email') {
             alert('Enter a valid email.');
             return false;
          }else if (errorCode === 'auth/wrong-password') {
             alert('Incorrect password.');
             return false;
          }else if (errorCode === 'auth/argument-error') {
             alert('Password must be string.');
             return false;
          }else if (errorCode === 'auth/user-not-found') {
             alert('No such user found.');
             return false;
          }else if (errorCode === 'auth/too-many-requests') {
             alert('Too many failed login attempts, please try after sometime.');
             return false;
          }else if (errorCode === 'auth/network-request-failed') {
             alert('Request timed out, please try again.');
             return false;
          }else {
             alert(errorMessage);
             return false;
          }
        });
    }else{
        alert('Please enter email and password');
        return false;
    }//end check client username password
  };// end $scope.doLogin()
}])
EN

回答 2

Stack Overflow用户

发布于 2017-03-28 22:35:42

我会回答我自己的问题,因为我找到了解决办法:

在我的例子中,您必须使用以下代码:

代码语言:javascript
复制
firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $state.go("tab.favorites");
    // User is signed in.
  } else {
    // No user is signed in.
  }
});
票数 2
EN

Stack Overflow用户

发布于 2017-03-28 15:18:32

尝试在登录后在sharedPreferences中或使用外部资源存储登录,然后在用户注销时删除值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43073371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档