Routes - AngularJS

0

I'm using angularjs 1.6 routes and I'm having problems with the java connection filter part.

When I log in to my system and access a page from my system, the filter works normally, but when I open another page without refreshing the page, the filter is ignored.

I think it's because of the url that the angular routes are mounting or something of the sort.

Taking a more explanatory example:

I log in to the system and access /users in the URL, the filter works correctly. Without doing the refresh on the screen, I access /appointments and the filter is ignored, it does not fall into the filter class debug.

I configured the routes this way:

/* Configuração de rotas */
app.config(function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl : 'home.html',
    })

    $routeProvider.when('/appointments', {
        templateUrl : 'views/appointment/appointment.html',
        controller : 'AppointmentController'
    })

    $routeProvider.when('/patients', {
        templateUrl : 'views/patient/patient.html',
        controller : 'PatientController'
    })

    $routeProvider.when('/users', {
        templateUrl : 'views/user/user.html',
        controller : 'UserController'
    })

    $routeProvider.when('/services', {
        templateUrl : 'views/service/service.html',
        controller : 'ServiceController'
    })
});
    
asked by anonymous 09.08.2017 / 04:10

1 answer

0

In order for you to create restricted access areas with AngularJS you can choose to use cookies. Use the sessionStorage property to store the user's session id or other data that identifies the user session. Example:

/* Aqui é gravado a sessão do usuário, neste exemplo vamos utilizar o
email como a sessão dele (Não vamos utilizar criptografia para não complicar o exemplo)*/

function setSession(email){
    sessionStorage.setItem("email", email);
}

/* Recupera o dado armazenado */
function getSession(){
    return sessionStorage.getItem("email");
}

/* Verifica se a sessão existe, caso não exista, redireciona para a pagina principal */
function verificaSessao($location){
    if(getSession() == null){
        $location.path('/');
    }
}

app.controller('AppointmentController', ['$scope', '$http', '$location', 
    function($scope, $http, $location){
    verificaSessao($location);
}]);
    
11.08.2017 / 18:23