this is my first time working with angular and after a lot of time spent on online tutorials and guides I ended up getting stuck in sessions management. Everithing works fine except that when I do the login the variable $_SESSION['uid']
contains what I want, but that value is not parsed back into the $promise.then(function(response){...});
and so I can't start the session properly.
Can you give me a clue?
Tutorial followed: Simple session with angularjs and php, angularJs
user.php
<?php
$json =file_get_contents('php://input');
$user=json_decode($json); //get user from JSON
if($user->mail=='email@gmail.com' && $user->pass=='1234')
session_start();
//$_SESSION['uid'] = uniqid('ang_');
$_SESSION['uid'] = $user->mail;
return $_SESSION['uid']; ?>
loginService.js
(function() {
'use strict';
var app = angular.module('loginS',['sessionS']);
app.factory('loginService',['$http','$location','sessionService',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(response){
console.log(response);
var uid = response.data;
if(uid!=""){
//scope.msgtext='Correct information';
sessionService.set('uid',uid);
$location.path('/home');
}
else{
scope.msgtext='Wrong information';
$location.path('/login');
}
});
},
logout:function(){
sessionService.destroy('uid');
$location.path('/login');
},
isLogged:function(){
var $checkSessionServer =$http.post('data/check_session.php');
return $checkSessionServer;
/*
if(sessionService.get('user')) return true;
else return false;
*/
}
}
}]);
})();
login.tmp.html
<div class="container">
<center>
<div class="bs-example text-left">
<form role="form" name="form1" >
<div class="form-group">
<p>Welcome : {{user.mail}} : {{user.pass}}</p>
<label for="exampleInputEmail1">Mail</label>
<input type="text" placeholder="Enrter name" class="form-control" id="exampleInputEmail1" required ng-model="user.mail"></input>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" placeholder="Password" class="form-control" id="exampleInputPassword1" required ng-model="user.pass"></input>
</div>
<button class="btn btn-default" ng-disabled="form1.$invalid" ng-click="login(user)">Submit</button>
<p>{{msgtext}}</p>
</form>
</div>
</center>
This is what I get from the promise.... I can't figure out why....
console.log
Object {data: "", status: 200, config: Object, statusText: "OK"}