App initilisation上的Firebase身份验证 [英] Firebase authentication on App initilisation

查看:114
本文介绍了App initilisation上的Firebase身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  console.log('User ID:'+ user.id +',Provider:' + user.provider); 

但这不是:

  $ scope.authenticated.currentUser = user.id; 

我的目标是采取一些认证变量(Email + UserID)他们访问配置文件节点ON火力点。在初始化我想要的应用程序的用户名,电子邮件,和其他一些我需要的东西。


$ b $ pre $ cross $ app $ control $范围,defautProfileData,$ q){

var dataRef =新的Firebase(https://glowing-fire-5401.firebaseIO.com);

$范围。 authenticated = {
currentUser:$ scope.authemail,
emailAddress:,
settings:,
};

var chatRef = new Firebase('https://< YOUR-FIREBASE> .firebaseio.com');
var auth = new FirebaseSimpleLogin(chatRef,function(error,user){$ b $ if(error){
//尝试登入时发生错误
switch(error.code){
case'INVALID_EMAIL':
case'INVALID_PASSWORD':
default:
}
} else if(user){
//用户使用Firebase
进行身份验证console.log('User ID:'+ user.id +',Provider:'+ user.provider);

$ scope.authenticated.currentUser = user.id; //

} else {
//用户注销
}
});

}]); // GlobaldCtrl


解决方案

Angular的HTML编译器的问题。



当你使用像ng-click / ng-submit / etc这样的事件时,Angular触发了$ scope。$ apply(),它检查你的$ scope变量的任何变化并将它们应用到DOM。由于FirebaseSimpleLogin不属于Angular的权限范围,因此不知道当回调被触发时,你已经更新了$ scope.authenticated.currentUser。这也可以解释为什么当你调用auth.login()时,它可以工作,因为你可能通过某个地方的ng-click事件来调用这个事件,这会触发一个摘要检查并且发现更改。

如果确实如此,您可以通过警告Angular来纠正这个问题,它需要使用$ timeout运行$ apply:

<$ p $ ($ scope,defautProfileData,$ q,$ timeout){code> crossfitApp.controller('globalIdCtrl',[$ scope,'defautProfileData','$ q','$ timeout'



var auth = new FirebaseSimpleLogin(chatRef,function(error,user){
if(error){
/ * ... * /
} else if(user){
$ timeout(function(){
$ scope.authenticated.currentUser = user.id; //
});
} else {
//用户注销
}
});


This is works:

console.log('User ID: ' + user.id + ', Provider: ' + user.provider);

but this one is not:

$scope.authenticated.currentUser = user.id;

My goal here is to take to take some of the authentication variables (Email+UserID) and then use them to access a profile node ON firebase. On initialization I want the username, email, and a few other things I need for the app.

crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', function ($scope,defautProfileData,$q) {

var  dataRef =   new Firebase("https://glowing-fire-5401.firebaseIO.com");

$scope.authenticated={
                        currentUser: $scope.authemail,
                        emailAddress: "",
                        settings: "",
                    };

var chatRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
  if (error) {
    // an error occurred while attempting login
    switch(error.code) {
      case 'INVALID_EMAIL':
      case 'INVALID_PASSWORD':
      default:
    }
  } else if (user) {
    // user authenticated with Firebase
    console.log('User ID: ' + user.id + ', Provider: ' + user.provider);

      $scope.authenticated.currentUser = user.id ;//

  } else {
    // user is logged out
  }
});

}]);  //GlobaldCtrl

解决方案

Most likely, you're running into a problem with Angular's HTML Compiler.

Whenever you use an event like ng-click/ng-submit/etc, Angular fires $scope.$apply(), which checks for any changes to your $scope variables and applies them to the DOM.

Since FirebaseSimpleLogin is not part of Angular's purview, it has no idea that when the callback is fired, you've updated $scope.authenticated.currentUser. This would also explain why it works when you call auth.login(), since you're probably invoking that via an ng-click event somewhere, which would fire a digest check and discover the changes.

If this is indeed the case, you can correct this issue by alerting Angular that it needs to run $apply by using $timeout:

crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', '$timeout', function ($scope,defautProfileData,$q, $timeout) {

/* ... */

var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
  if (error) {
    /* ... */
  } else if (user) {
    $timeout(function() {
       $scope.authenticated.currentUser = user.id ;//
    });
  } else {
    // user is logged out
  }
});

这篇关于App initilisation上的Firebase身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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