如何获得经过验证的用户信息并在所有控制器和服务中使用? [英] How to get the authenticated user info and use in all controllers and services?

查看:161
本文介绍了如何获得经过验证的用户信息并在所有控制器和服务中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是angularFireAuth,我想检索登录用户的信息,并在
中使用所有的控制器或服务,当应用程序是初始。



目前,我在每个控制器中都使用了这个函数,但是我遇到了一些问题。
$ b $ $ p $ $ $ $(angularFireAuth:login ,函数(evt,user){
console.log(user);
});

如果不是整页加载,或者应用程序初始化时返回null,则不会调用该回调函数。 / p>

我需要一些提示,指出如何返回经过身份验证的用户信息,以便在应用程序初始化时以及在所有控制器和服务中使用。 b

示例

在控制器或服务中时


  • $ scope.auth.user.id 将会返回用户的标识符

  • $ scope.auth.user。名称将返回用户名称

  • etc


解决方案

我会从 userService 开始:

  angular.module('EventBaseApp')。service('userService',function userService(){
return {$ b $ isLogged:false,
username:null
}

});

然后写一个 LoginCtrl 函数($ scope,userService,angularFireAuth)
。控制器('LoginCtrl',函数($ scope,userService,angularFireAuth)
$ b $ $ p $ angular.module('EventBaseApp' {
var url =https://example.firebaseio.com;
angularFireAuth.initialize(url,{scope:$ scope,name:user});

$ scope.login = function(){
angularFireAuth.login(github);

};
$ scope.logout = function(){
)angularFireAuth.logout();
};

$ scope。$ on(angularFireAuth:login,function(evt,user){
userService.username = $ scope .user;
userService.isLogged = true;
));

$ scope。$ on(angularFireAuth:logout,function(evt){
userService .isLogged = false;
userService.username = null;
});
});

在用户的任何位置注入 userService



我目前正在使用的应用程序使用这个 - https://github.com/manojlds/EventBase/blob/master/app/scripts/controllers/login.js



基于这里提出的想法 - http://blog.brunoscopelliti.com/deal-with-users-authentication-in-an-angularjs-web-app


I'm using angularFireAuth and I want to retrieve the logged in user's info and use in all the controllers or services when the app is initial.

Currently, I used this in every controller but i having some problem.

$scope.$on("angularFireAuth:login", function(evt, user){ 
  console.log(user);
});

The callback will not call if it is not a full page load or return null when app initial.

I need some tips for how can I return the authenticated user's info so I can use when app is initial and in all the controllers and services.

Example

When in controller or services

  • $scope.auth.user.id will return user's ID
  • $scope.auth.user.name will return user's name
  • etc

解决方案

I would start with a userService for this:

angular.module('EventBaseApp').service('userService', function userService() {
    return {
        isLogged: false,
        username: null
    }

});

And write a LoginCtrl controller:

angular.module('EventBaseApp')
  .controller('LoginCtrl', function ($scope, userService, angularFireAuth) {
    var url = "https://example.firebaseio.com";
    angularFireAuth.initialize(url, {scope: $scope, name: "user"});

    $scope.login = function() {
        angularFireAuth.login("github");

    };
    $scope.logout = function() {
        angularFireAuth.logout();
    };

    $scope.$on("angularFireAuth:login", function(evt, user) {
      userService.username = $scope.user;
      userService.isLogged = true;
    });

    $scope.$on("angularFireAuth:logout", function(evt) {
      userService.isLogged = false;
      userService.username = null;
    });
});

Inject the userService anywhere you want the user.

My app that am currently working on that uses this - https://github.com/manojlds/EventBase/blob/master/app/scripts/controllers/login.js

Based on ideas presented here - http://blog.brunoscopelliti.com/deal-with-users-authentication-in-an-angularjs-web-app

这篇关于如何获得经过验证的用户信息并在所有控制器和服务中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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