0

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"}
L4ZZA
  • 83
  • 12
  • Consider searching this site for other discussions of "promise" in AngularJS, e.g. http://stackoverflow.com/questions/14080095/angularjs-promise. Also review "AngularJS Promises: The Definitive Guide," at http://www.codeproject.com/Articles/770325/AngularJS-Promises-The-Definitive-Guide – Mike Robinson Mar 27 '16 at 13:34

2 Answers2

1

return in php does not output anything to browser.... you need to use echo.

in addition the default expected Content type for $http is json so I would change:

return $_SESSION['uid']; 

To something like:

$output = array('uid'=>$_SESSION['uid']);

echo json_encode($output);

Then in angular:

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.uid;
            if(uid){
                //scope.msgtext='Correct information';
                sessionService.set('uid',uid);
                $location.path('/home');

            }
            else{
                scope.msgtext='Wrong information';
                $location.path('/login');
            } 

        });
    },
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

After your answer I've tried this because I understand it better and it works too.

Instead of:

$output = array('uid'=>$_SESSION['uid']);

echo json_encode($output);

I've used this:

$_SESSION['uid'] = $user->mail;
echo $_SESSION['uid'];

So in Angular:

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');
        } 

    });
}
L4ZZA
  • 83
  • 12