i'm new at angular ui-router, and i'd like to add a login page to my application, so this is my code
var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state("login", {url:"/login",module: 'public',templateUrl: 'partials/login.html', controller: 'loginCtrl'})
.state("home", {url:"/home", module: 'private',templateUrl: 'partials/home.html', controller: 'homeCtrl'})
.state("home.client", { url:"/client", parent: 'home', module: 'private',templateUrl:"pages/client.html", controller:'clientCtrl' })
.state("home.facture", { url:"/facture", parent: 'home', module: 'private',templateUrl:"pages/facture.html", controller:'factCtrl' })
.state("home.etat", { url:"/etat", parent: 'home', module: 'private',templateUrl:"pages/etat.html", controller:'etatCtrl' });
$urlRouterProvider.otherwise("/login");
});
app.run(function($rootScope, $location, loginService){
var routespermission=['/home']; //route that require login
$rootScope.$on('$stateChangeStart', function(){
if( routespermission.indexOf(location.path()) !=-1)
{
var connected=loginService.islogged();
connected.then(function(msg){
if(!msg.data) $location.path('/login');
});
}
});
});
and these are my services
app.factory('loginService', function($http, $location, sessionService){
return{
login:function(data,scope){
var $promise=$http.post('data/user.php',data); //send data to user.php
$promise.then(function(msg){
var uid=msg.data;
if(uid){
sessionService.set('uid',uid);
$location.path('/home/etat');
}
else {
$location.path('/login');
}
});
},
logout:function(){
sessionService.destroy('uid');
$location.path('/login');
},
islogged:function(){
var $checkSessionServer=$http.post('data/check_session.php');
return $checkSessionServer;
}
}
});
app.factory('sessionService', function($http){
return{
set:function(key,value){
return sessionStorage.setItem(key,value);
},
get:function(key){
return sessionStorage.getItem(key);
},
destroy:function(key){
$http.post('data/destroy_session.php');
return sessionStorage.removeItem(key);
}
};
});
The problem is that if i try to log into my application using any username and password, it always logg in...and if i log out, i can get back to the home page with a simple press on the previous button :( Can you help me, please...i just started using angularJs since one week.
If it can help, these are the used php files
//user.php
$user=json_decode(file_get_contents('php://input'));
if($user->mail=='admin' && $user->pass=='admin')
session_start();
$_SESSION['uid']=uniqid('ang_');
print $_SESSION['uid'];
//check_session.php
session_start();
if( isset($_SESSION['uid']) ) print 'authentified';
//destroy.php
session_id('uid');
session_start();
session_destroy();
session_commit();