Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cbil360's avatar

Laravel sessions for API application

I added a question but,that was too long and now I am trying to be specific and to the point. I have a API workflow and structure of app is as below

 --ProjectDir
   |_angularDir
     |_Start
        |_bower_components
        |_node_modules          //front end dependencies
        |_gruntfile.js
  |_public                    //laravel public dir

We are using * grunt * as a build tool and it launches the app on http://localhost:9000. Now this takes me to a route http://localhost:9000/#/login. After I enter user name and password,I call the laravel route which is localhost/project_dir/public/login

   Route::post('login','LoginController@auth');

Controller

 public function auth()
  {  
     $rowInput =Request::all();
     $input = \Request::only('customer.email', 'customer.password');
     if(Auth::attempt(array_get($input, 'customer')))
   {
   //var_dump(Session::all());
    return \Response::json(['status'=>'success','url'=>$rowInput['customer']['redirectURL']]);
    }else {
    return \Response::json(['status'=>'fail','url'=>$rowInput['customer']['redirectURL']]);    
   }
}

The app enters in to the attempt method successfully,laravel creates a session,but then it does not persist. The user is redirected back to the login page.

I understand that I am sending request from a different domain (localhost:9000) and the response is from a different domain(localhost/proj_dir/public), but how can I handle these routes for this kind of workflow with laravel sessions.

The app worked fine till I had everything inside the public folder of laravel. But as I switched to this workflow it has created problems with sessions and I am stuck on this for a couple of days. PLease suggest the correct direction and if needed updates in the workflow.

Request Response headers

      Remote Address:127.0.0.1:80
 Request URL:http://localhost/project_dir/public/user/check
 Request Method:POST
 Status Code:200 OK
 Response Headers
 view source
 Access-Control-Allow-Origin:http://localhost:9000
 Cache-Control:no-cache
 Connection:Keep-Alive
 Content-Length:4
 Content-Type:text/html; charset=UTF-8
 Date:Wed, 29 Jul 2015 10:30:33 GMT
 Keep-Alive:timeout=5, max=99
 Server:Apache/2.4.9 (Win64) PHP/5.5.12
 Set-  Cookie:laravel_session=fgdgdgdfgfgdxUDRra3E1NXdcL25OekJvSlBRPT0iLCJ2YWx1ZSI6IlVaWGF6NmdsV1wvZ3NcL1FTVkR5Y2J3czlQNUkyaWdxZlJmeG5zK2U2c0lqM2VOMEY5S000cVwveTBVazBwVFNSZXlhZmhWSEUxUlRyMVJidE5TcE5jM2FdsfsfdsfsfsdfsfsdfsfzIn0%3D; expires=Wed, 29-Jul-2015 12:30:33 GMT; Max-Age=7200; path=/; domain=http://localhost:9000/;  httponly
 Vary:Origin
 X-Powered-By:PHP/5.5.12
 Request Headers
 view source
 Accept:application/json, text/plain, */*
 Accept-Encoding:gzip, deflate
 Accept-Language:en-US,en;q=0.8
 Cache-Control:max-age=0
 Connection:keep-alive
 Content-Length:0
 Host:localhost
 Origin:http://localhost:9000
 Referer:http://localhost:9000/
 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36   (KHTML,  like Gecko) Chrome/44.0.2403.107 Safari/537.36          FirePHP/4Chrome
 X-FirePHP-Version:0.0.6
 X-Wf-Max-Combined-Size:261120
0 likes
2 replies
cbil360's avatar

@odin88 No,actually this way it wont work as grunt starts its own server on a different post and apache runs on a different port,so its like two diff asplications communicating,which is logically wrong.They should work together.We had to make changes to our workflow a bit. THis is good if we have to just test the front end of the application

Please or to participate in this conversation.