Access to html view with or without being logged in Angularjs

0

In my $ stateProvider I have defined the views of my application in which certain views require that the user is logged in or not. So far, he is working correctly. But the problem I have now is that I have a view which the user must access with or without login, this is my code:

.config(function($stateProvider, $urlRouterProvider){
        $stateProvider
            .state('registro',{
                url:'/registro',
                templateUrl:'app/carpeta1/vista1.html',
                controller: 'registroCtrl',
                authenticate: true
            })
            .state('registro_emp',{
                url:'/registro_emp',
                templateUrl:'app/carpeta2/vista2.html',
                controller: 'registro_empCtrl',
                authenticate: false
            })
            .state('empresas', {
                url:'/empresas/:id_empresa',
                templateUrl:'app/carpeta3/vista3.html',
                controller: 'perfilEmpCtrl_sl',
                authenticate: true/false (aqui deseo llamar a una factoria la cual me devuelve si el usario esta o no logueado)
            })
        $urlRouterProvider.otherwise('/login');
    })

this is my factory:

.factory("Autenticacion", function($cookies, $cookieStore)
{
    return{
        isAuthenticated : function()
        {
            var username = $cookieStore.get('username');
            if(typeof(username) != "undefined")
            {   
                return true;
            }else{
                return false;
            }
        }
    }
})

My question is how to insert the factory that returns my login status in the state "companies" > since that view can be accessed by the user with login or without login

Thank you in advance

    
asked by Dimoreno 18.09.2017 в 19:10
source

1 answer

0

With this, you get the check up before entering, if it is correct, you execute whatever you want.

.state('empresas', {
    url: '/empresas/:id_empresa',
    templateUrl: 'app/carpeta3/vista3.html',
    controller: 'perfilEmpCtrl_sl',
    onEnter: function($state, Autenticacion){

        if(Auth.isAuthenticated()){
            /*** Y aqui el codigo para cuando este autenticado ***/
        }
    }        

})

This I use for when I enter the login, when opening an app if it is already logged in because its token has not expired, it is forwarded to the homepage, otherwise I run the login driver

    
answered by 18.09.2017 в 19:31