【问题标题】:Redirect after email authentication电子邮件身份验证后重定向
【发布时间】:2016-01-22 22:36:08
【问题描述】:

成功登录后重定向用户时遇到问题,我已阅读 Firebase 的文档并尝试了几件事,但到目前为止没有运气

谁能指出我正确的方向?

提前致谢, 杰雷米。

这是controller.js

.controller('LoginCtrl', function($scope, $ionicPopup, $state, Auth) {
$scope.data = {};

$scope.login = function() {
      Auth.login($scope.data.email, $scope.data.password).then(function() {
        $state.go("tab-discover");
               })

    .error(function() {
        var alertPopup = $ionicPopup.show({
            title: 'Mauvais identifiants',
            template: 'Veuillez recommencer'
        });
    });
}

$scope.signup = function() {
      Auth.signup($scope.data.email, $scope.data.password)
    .error(function() {
        var alertPopup = $ionicPopup.show({
            title: 'Erreur',
            template: 'Un probleme est survenu'
        });
      });
    }

  })

还有 services.js

.factory("Auth", function(FURL, $firebaseAuth) {


  var ref = new Firebase(FURL);
  var auth = $firebaseAuth(ref);
  var Auth = {
    user: {},

login: function(email, password){
  console.log("loginService", email, password);

  return ref.authWithPassword({
    "email": email,
    "password": password
  }, function(error, authData) {


    if (error) {
    console.log("La connexion a echoué!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
  }
})
    },
    signup: function(email, password){
      console.log("signupService", email, password);
      return ref.createUser({
        "email": email,
        "password": password
      }, function(error, userData) {
  if (error) {
    switch (error.code) {
      case "EMAIL_TAKEN":
        console.log("The new user account cannot be created because the email is already in use.");
        break;
      case "INVALID_EMAIL":
        console.log("The specified email is not a valid email.");
        break;
      default:
        console.log("Error creating user:", error);
    }
  } else {
    console.log("Successfully created user account with uid:", userData.uid);
  }
}).then(function(){
        return Auth.login(email, password);
    })
  }
}
return Auth;
})

【问题讨论】:

  • 重定向,如window.location = 'someotherlocation';?
  • @Filipe OP 正在使用角度路由来重定向 $state.go(..) 行

标签: angularjs firebase angularfire firebase-authentication


【解决方案1】:

看起来 firebase 正在使用回调,您试图将其作为 then 的承诺返回。一个简单的解决方法是将回调传递给您的登录函数并在 firebase 回调中调用它:

login: function(email, password, callback, onError){
  console.log("loginService", email, password);

  ref.authWithPassword({
    "email": email,
    "password": password
  }, function(error, authData) {

  if (error) {
    onError(error);
  } else {
    callback(authData);
  }
})

然后这样称呼它:

Auth.login($scope.data.email, $scope.data.password, function (data) {
    console.log("Authenticated successfully with payload:", data);
    $state.go("tab-discover");
}, function (error) {
    console.log("La connexion a echoué!", error);
});

【讨论】:

  • 我得到 750853 错误 TypeError: Cannot read property 'then' of undefined at Scope.$scope.login (localhost:8100/js/controllers.js:7:62) 和 751852 warn FIREBASE WARNING: 异常被用户回调抛出。 TypeError:回调不是函数
  • 我从代码中删除了 then,如果您收到该错误,则不是我提供的代码
猜你喜欢
  • 2023-03-18
  • 2012-08-10
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2018-10-09
  • 2016-12-27
  • 1970-01-01
相关资源
最近更新 更多