Routes - Angularjs

Asked

Viewed 180 times

0

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

When I log into my system and access a page of 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 Angularjs routes are mounting or something like that.

Giving a more explanatory example:

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

I set 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'
    })
});
  • The filter is ignored in the sense that it does not go pro appointments or he goes without showing the login?

  • In the sense of not respecting the filter... I need the filter to be applied to every page access. I need to perform the connection filter, when the user tries to access /appointments, understood?

  • Think of the following scenario... the user logged on to the system, accessed /users and was a while without interaction with the system and the session expired. When it accesses /appointments, the session will not be active and it should be redirected to the login page.

1 answer

0


So you can create restricted areas with Angularjs you can choose to use cookies. Use the property sessionStorage to store the user’s session id or other data identifying the session of the user. 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);
}]);

Browser other questions tagged

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