I'm getting the following error:
Uncaught TypeError: loginService.verificarSesion(...).then is not a function
I think I don't understand perfectly promise
I need to check if the user is logged in on deviceIsReady on my apache-cordova app. This is the part of the function:
.run(function($ionicPlatform, $http, $state, loginService) {
$ionicPlatform.ready(function() {
loginService.verificarSesion()
.then(function(usuario) {
if(usuario == 1){
//es complejo
$state.go('app.home');
}
else if(usuario == 2){
//es usuario comun
$state.go('app.home-usuario');
}
else{
//no se renococió ningún usuario válido
$state.go('app.login');
}
})
.catch(function(e){
console.log('error trying to find the user');
});
});
})
.config(function($urlRouterProvider) {
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/login');
});
Service:
(function() {
'use strict';
angular
.module('example.login')
.factory('loginService', loginService);
loginService.$inject = ['$http','remoteDataService', '$q','_','localStorageService'];
/* @ngInject */
function loginService($http, remoteDataService, $q,_, localStorageService){
var token = 'token';
var usuario = 'usuario';
var service = {
verificarSesion: verificarSesion
};
return service;
//funcion para saber si hay una sesión activa
function verificarSesion() {
var usuariologueado = localStorageService.get(token) || [];
if (usuariologueado == []){
return [];
}
return localStorageService.get(usuario);
}
//generar error si hubo un problema
function generarError(e){
if (e.message) {
return $q.reject(e.message);
}
return $q.reject('Hubo un problema al conectarse al servidor. Intente nuevamente.');
}
}
})();
If the user is logged in I have to sen the user to another view.
What I'm doing wrong? Thanks!