【问题标题】:Ionic/Firebase Success function after Auth身份验证后的 Ionic/Firebase 成功功能
【发布时间】:2017-06-16 19:16:45
【问题描述】:

我有这个登录功能,它正在工作。但我只在没有发现错误的情况下才尝试重定向。我在下面尝试了这段代码,但这并没有按照我想要的方式工作,任何想法,比如我不知道的成功函数?

$scope.loginEmail = function($email, $password){
 
var testing = 1;
$scope.error = '';

firebase.auth().signInWithEmailAndPassword($email, $password).catch(function(error) {

  var errorCode = error.code;
  var errorMessage = error.message;
  $scope.error = errorMessage;
  testing = 0;
  console.log("Testing 1 : ", testing);
  
});

if(testing === 1)
{
    console.log("Testing 2 : ", testing);
    $state.go("menu.VNements");
}
};

【问题讨论】:

    标签: javascript angularjs firebase ionic-framework firebase-authentication


    【解决方案1】:

    该函数返回一个Promise - catch 表示错误,then 表示成功。

    function signInSuccess(response) {
      console.log('signInSuccess', response);
    }
    
    function signInError(response) {
      console.log('signInError', response);
    }
    
    firebase.auth().signInWithEmailAndPassword($email, $password)
      .then(signInSuccess);
      .catch(signInError);
    

    【讨论】:

    • 这确实可行。 Firebase 文档没有强调这种方法的原因是,当用户重新访问同一页面时,您通常不希望他们再次登录。通过listening for onAuthStateChanged()(而不是登录方式的then()),可以获取现有的auth状态。
    • 我很好奇,您将如何使用现有的身份验证状态?你能给我举个例子吗?
    【解决方案2】:

    为了更清楚,我把我的代码放在这里,感谢 Dominic Tobias!

    $scope.loginEmail = function($email, $password){
     
        function signInSuccess(response) {
            console.log('signInSuccess : ', response);
            $state.go("menu.VNements");
        }
    
        function signInError(response) {
            console.log('signInError : ', response);
        }
    
    firebase.auth().signInWithEmailAndPassword($email, $password)
      .then(signInSuccess)
      .catch(signInError);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      相关资源
      最近更新 更多