首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularFire authWithPassword不在角工厂工作

AngularFire authWithPassword不在角工厂工作
EN

Stack Overflow用户
提问于 2014-12-24 04:23:21
回答 3查看 995关注 0票数 0
代码语言:javascript
复制
  app.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
  var ref = new Firebase("https://XXXXXX.firebaseio.com/");
  var auth = $firebaseAuth(ref);

  var Auth = {
    login: function(){
      return auth.$authWithPassword({
        "email": "1@1.com", //hard-coding in the email & password for testing
        "password": "1"
      }, function(error, authData) {
        if (error) {
          console.log("Login Failed!", error);
        } else {
          console.log("Login Success!", authData);
        }
      });
    }
  return Auth;
}]);

app.controller("SampleCtrl", ["$scope", "Auth", "$firebaseAuth", function($scope, Auth, $firebaseAuth) {
  $scope.login = Auth.login();
}]);

我正在尝试创建一个工厂来使用angularFire用户身份验证功能。

代码语言:javascript
复制
<button ng-click="login()">Login</button>

它返回一个错误:TypeError: Cannot read property '0' of null,我做错了什么?柱塞

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-30 02:10:56

代码语言:javascript
复制
app.controller("loginCtrl", ["$scope", "Auth", function($scope, Auth){
  $scope.login = function(){
    Auth.login().then(function(){
      console.log("OK");  
    });
  };
}]);

不知何故,这是通过将Auth.login()封装在一个函数中,并在其后面加上一个then来实现的。有人能解释一下为什么会起作用吗?

票数 0
EN

Stack Overflow用户

发布于 2016-01-16 17:36:53

使用AngularFire时,使用密码提供程序登录的调用应该如下所示:

代码语言:javascript
复制
var ref = new Firebase("url","name");
   var svcAuth = return $firebaseAuth(ref); 
   svcAuth.$authWithPassword({
                        email: email,
                        password: "******"})
                        .then(onLogon)
                        .catch(onError);

   function onLogon(authData) {
            console.log("Authenticated payload:", authData);                
    }

    function onError(error) {
            console.log("Authentication error:", error);
    }

如果您没有使用AngularFire (仅使用Firebase.js ),则登录代码应如下所示:

代码语言:javascript
复制
var ref = new Firebase("url");
ref.authWithPassword({
  email    : "email",
  password : "*****"
}, function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated payload:", authData);
  }
});

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2014-12-29 03:58:05

试试看它对我有用:

代码语言:javascript
复制
angular.module('database-operations').factory('Auth', ["$firebaseAuth", function($firebaseAuth) {
        var ref = new Firebase("https://XXXXX.firebaseio.com/");
        var auth = $firebaseAuth(ref);

     return{
            login: function(){
                return auth.$authWithPassword({
                    "email": "1@1.com", //hard-coding in the email & password for testing
                    "password": "1"
                }).then(function(authData){
                    console.log('Login successful',authData.uid);
                }).catch(function(error) {
                    console.error("Authentication failed:", error);
                });
            }

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

https://stackoverflow.com/questions/27631439

复制
相关文章

相似问题

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