0

I'm trying to verify that when user try to acces login page, the system redirect to profile if user is logged. To do this I resolve a promise from service that return status user, but the function that return status is undefined. What am I doing wrong?

enter image description here

note 1: at the resolve from last state I try to doing by .then, I don't know if the rest work without .then note 2:extra factories in service doesn't matter

route.js

angular.module('Horarios.routes', ['Roles.constant','Session.service'])

.config(['roles', '$stateProvider', '$urlRouterProvider', function(roles, $stateProvider, $urlRouterProvider) {


$urlRouterProvider.otherwise('/loginr');

$stateProvider
.state('sidenav', {
    url: "/nav",
    templateUrl: 'templates/sidenav.html',
    controller: 'sidenavCtrl',
    controllerAs:'side'
})

.state('loginr', {
    url: "/loginr",
    templateUrl: 'templates/loginr.html',
    controller: 'loginCtrl',
    controllerAs:'login',
    resolve:{
        logeado : function(sessionFactory) {
            return sessionFactory.isLogeado().then(function (response) {
                return response;
            });
        }
    }
})
}])

service

angular.module('Session.service', [])

.factory('sessionFactory',['$q','$http',  function($q,$http) {
self = this;

self.isLogeado = function(){
    var promesa = $q.defer();
    $http.post('scripts/islogeado.php').then(function(respuesta){
        promesa.resolve(respuesta);
    })
    return promesa.promise;
}

self.autentica = function(){
    var promesa = $q.defer();
    var url = "scripts/autentica.php";
    $http.post(url).then(function(respuesta){
        console.log(respuesta.data);
        var usuario = respuesta.data;
        console.log(usuario.nombre);
        var primerNomb = usuario.nombre.indexOf(" ");
        var primerApell = usuario.apellido.indexOf(" ");
        var nombre = (primerNomb>0)? usuario.nombre.substring(0,primerNomb) : nombre = usuario.nombre;
        var apellido = (primerApell>0)? usuario.apellido.substring(0,primerApell) : apellido = usuario.apellido ;
        usuario.nombreApellido = nombre+" "+apellido;
        console.log(primerApell);
        promesa.resolve(usuario);
    }, function(err){
        console.log("error al consultar usuario: "+err.message);
        promesa.reject(usuario);
    })

    return promesa.promise;
}

self.cierraSesion = function(){
    var promesa = $q.defer();
    $http.post('scripts/cerrar.php').then(function(){
        promesa.resolve("cierra");
    })
    return promesa.promise;
}
return self;
}]);

app.js

angular.module('horariosapp', [
    'ui.router',
    'Horarios.routes',
    'Mihorario.controller'
    ])

.run(['$rootScope', '$location', 'roles', function($rootScope, $location, roles){
$rootScope.$on('$stateChangeStart', function (event, next){
    console.log("funcion run");
    console.log(next);
    if(next.resolve.logeado()=='noEntra'){
        $state.go('loginr');
    }else if (next.data !== undefined){
        if (next.data.permisos.indexof(loginFactory.permiso)){
            console.log("is worth");
        }else{
            $state.go('sidenav.perfil');
        }
    } 
})
}])
AndresChica
  • 155
  • 1
  • 13
  • In the example, there are four resolves for `logeado`, done three different ways. Which ones are giving problems? – georgeawg Apr 16 '17 at 20:03
  • None works, I explained that I tried by a promise, and without promise – AndresChica Apr 16 '17 at 20:08
  • When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. [See How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – georgeawg Apr 16 '17 at 21:03

0 Answers0