Modifying my Isauthenticated value in Angularjs

Asked

Viewed 42 times

2

When the token is different from null my Isauthenticated has to be positive,

app.controller("HomeCtrl", function ($scope, $location) {
    let token = localStorage.getItem("token");
    $scope.user = JSON.parse(token);

    if (token === null) {
        $scope.IsAuthenticated = false;
        $('body').removeClass('nav-md');
        $('body').addClass('login');
        $location.path('/login');
        console.log($scope.IsAuthenticated);
    } else{
        $scope.IsAuthenticated = true;
        $('body').removeClass('login');
        $('body').addClass('nav-md');
        $location.path('/');
        console.log($scope.IsAuthenticated);
    }

    $scope.doLogout = function () {
        localStorage.removeItem('token');
        $scope.IsAuthenticated = false;
        $location.path('/login');
        console.log("chamou aqui");
        location.reload();
    }
});

even when the user logs my Isauthenticated value to false. how do I fix the problem? Below is my Loginctrl code

app.controller("LoginCtrl", function ($scope, LoginAPI, $location) {

    $scope.doLogin = function (model) {
        if (model.username === undefined || model.password === undefined) {
            return false;
        }

        LoginAPI.post(model).success(function (results) {
            localStorage.removeItem('token');
            //Armazena o token no localStorage
            localStorage.setItem('token', JSON.stringify(results));
            //preciso que o meu IsAuthenticated fique true antes de executar o comando abaixo
            $location.path('/');
        })
         .error(function (Error) {
             console.log(Error);
         }
    };
});
  • There may be a problem with the scope declared by let. How about using var instead?

  • put var and did not help

  • The variable token is being created?

  • Yes, I can log in perfectly, I enter the panel. Only as soon as I log in, I want my Authenticated to be positive as soon as I log in. The problem that this happening is that I soon, and I have to update the page once for it to stay true

  • So, use the service to share the same information between controllers, that is, isAuthenticated = true. Change the value of the variable in the service and take this value from the controller HomeCtrl. Edit: you will have to inject the service in HomeCtrl.

2 answers

2


In place of $location.path('/'); just put window.location.href = '/home/index'; Because as you are logging in, the first moment the page should be updated and reloaded. By doing this the problem will be solved.

2

This happens because you are assigning value by Loginctrl, the value has to be passed by Homectrl which by what I understand and your main Scope

  • I tend to agree. It would be better to build in Loginapi service an expression of the kind $scope.isAuthenticated = true and then use a third party to verify that the user is authenticated on each route.

  • This is also an option, do something like a Route Guard, if you have an ngRoute you may be doing the validation whenever you change route in the resolve{}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.