angularjs - How to get the authenticated user info and use in all controllers and services? -
i'm using angularfireauth , want retrieve logged in user's info , use in controllers or services when app initial.
currently, used in every controller having problem.
$scope.$on("angularfireauth:login", function(evt, user){ console.log(user); });
the callback not call if not full page load or return null when app initial.
i need tips how can return authenticated user's info can use when app initial , in controllers , services.
example
when in controller or services
$scope.auth.user.id
return user's id$scope.auth.user.name
return user's name- etc
i start userservice
this:
angular.module('eventbaseapp').service('userservice', function userservice() { return { islogged: false, username: null } });
and write 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 userservice
anywhere want user.
my app working on uses - 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
Comments
Post a Comment