我会从一个开始userService
:
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
任何你想要的用户。
我目前正在使用的应用程序使用这个 - 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