title: How to handle $rootscope and take user id of logged in user in AngularJS?
body: <p>I want to find the ID of the logged in user and display it in a page. I am new to angular and I don't have much clue on how to handle a session..</p>
  
  <p>I have an angular app which is connected to backend API (.net core).</p>
  
  <p>I will show the instances where $rootScope is used in the website (login and authorization is already enabled). I need to get an understanding of this to learn the app.</p>
  
  <p>In App.js :</p>
  
  <pre><code>//Run phase
  myApp.run(function($rootScope, $state) {
      $rootScope.$state = $state; //Get state info in view
  
      //Should below code be using rootScope or localStorage.. Check which one is better and why.
  
      if (window.sessionStorage["userInfo"]) {
  
          $rootScope.userInfo = JSON.parse(window.sessionStorage["userInfo"]);
      }
  
      //Check session and redirect to specific page
      $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
          if(toState &amp;&amp; toState.data &amp;&amp; toState.data.auth &amp;&amp; !window.sessionStorage["userInfo"]){
              event.preventDefault();
              window.location.href = "#login";
          }       
  
          if(!toState &amp;&amp; !toState.data &amp;&amp; !toState.data.auth &amp;&amp; window.sessionStorage["userInfo"]){
              event.preventDefault();
              window.location.href = "#dashboard";
          }
      });
  
  
  });
  </code></pre>
  
  <p>Users.js :</p>
  
  <pre><code>'use strict';
  angular.module('users', []);
  
  //Routers
  myApp.config(function($stateProvider) {
  
    //Login
    $stateProvider.state('login', {
      url: "/login",
      templateUrl: 'partials/users/login.html',
      controller: 'loginController'
    });
  //Factories
  myApp.factory('userServices', ['$http', function($http) {
  
      var factoryDefinitions = {
          login: function (loginReq) {
              $http.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
            return $http.post('http://localhost:1783/api/token?UserName='+loginReq.username+'&amp;password='+loginReq.password).success(function (data) { return data; });
        }
  }
  
      return factoryDefinitions;
    }
  ]);
  
  //Controllers
  myApp.controller('loginController', ['$scope', 'userServices', '$location', '$rootScope', function($scope, userServices, $location, $rootScope) {
  
      $scope.doLogin = function() {
  
          if ($scope.loginForm.$valid) {  
              userServices.login($scope.login).then(function(result){
                  $scope.data = result;
                  if (!result.error) {
                      window.sessionStorage["userInfo"] = JSON.stringify(result.data);
                      $rootScope.userInfo = JSON.parse(window.sessionStorage["userInfo"]);
                      //$localStorage.currentUser = { username: login.username, token: result.data };
                      //$http.defaults.headers.common.Authorization = 'Token ' + response.token;
                      $location.path("/dashboard");
                  }
              }); 
          }
      };
  }]);
  </code></pre>
  
  <p>I came to know that the information about the user will be available in $rootScope.userInfo. If so, how can I take a value inside it?</p>
  
  <p>Please explain with an example if possible. Thanks in advance.</p>