Z
Z
zigen2016-04-05 15:53:07
Django
zigen, 2016-04-05 15:53:07

How to check if a user is logged in angular?

Good afternoon.
I want to check whether the user is authorized in the template and, based on this, show or hide navigation elements. The template is like this:

<ul class="nav nav-tabs" ng-controller="AuthController">
      {% verbatim %}
      <li ng-class="getClass('/')"><a href="#/">Главная</a></li>
      <li ng-hide="auth" ng-class="getClass('/login')"><a href="#/login">Войти</a></li>
      <li ng-show="auth" ng-class="getClass('/logout')"><a href="#/logout" ng-click="logout()">Выйти</a></li>
      <li ><a href="">{{ auth }}</a></li>
      {% endverbatim %}
    </ul>

Controller:
function AuthController($scope, $location, $http){
    $scope.logout = function() {
      $http.get('api/auth/logout/').success(function(){
      $scope.auth = false;
      $location.path('/login');
      });
    };
    $scope.login = function() {
    $http({
        method: 'POST',
        url: 'api/auth/login/',
        data: $scope.user
    })
    .success(function(data){
        $scope.auth = true;
        $location.path('/');
    });
    }
}

After successful authentication, I create a variable in the scope with the username. In the case of a logout, I accordingly assign undefined.
However, in the template, the auth variable is empty, $scope.auth = data.username; works correctly

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question