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 protected]' && $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"}
1

2 Answers 2

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

        });
    },
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it works now! Could you tell me why in the video tutorial that works though?
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');
        } 

    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.