且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取经过身份验证的用户信息并在所有控制器和服务中使用?

更新时间:2023-12-02 14:39:34

为此,我会从 userService 开始:

I would start with a userService for this:

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

});

并编写一个LoginCtrl控制器:

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;
    });
});

userService 注入到您想要用户的任何位置.

Inject the userService anywhere you want the user.

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

My app that am currently working on that uses this - 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

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