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 usingvar
instead?– Not The Real Hemingway
put var and did not help
– Jefferson Souza
The variable
token
is being created?– Not The Real Hemingway
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
– Jefferson Souza
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 controllerHomeCtrl
. Edit: you will have to inject the service inHomeCtrl
.– Not The Real Hemingway