0

I'm experiencing a problem with sessions in Laravel. My project consists about two projects, one an API and another a WebApp. Both with Laravel 5.5.

The problem is that I want to save a session in my API project but it isn't saved. I save the session like this in api.php:

Route::get('test', function () {
    session(['data' => "data"]);
    session()->save();
});
  • If I visit: http://mydomain.dev/test through Firefox, I can see the session in the Laravel DebugBar because it has been saved:

enter image description here

  • If I make a request with Postman to that URL, session doesn't appear in the Laravel DebugBar! It isn't saved.

After some research, I found this question and people say to include in Kernel.php these two lines:

protected $middleware = [
        //...
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

I have already added those two lines and the problem persists.

The same happens with Guzzle. From my WebApp, I make a GET call to my API. In the method called in the API, I save a session, and when I retrieve the session in another method of the API, I get null because session hasn't been saved!

My suspicion is that Postman and Guzzle problems with sessions are related, and that there's something I'm missing.

My config\session.php files are as default. I know I have as alternative to save sessions in database, but I would prefer to keep it as default, but if I don't have any alternative, I will change my SESSION_DRIVER option from session.php from file to database.

Sergio
  • 317
  • 1
  • 7
  • 18
  • 1
    APIs are and should be session less. They can not handle it. That's why token based authentications are used. – Afraz Ahmad Aug 18 '18 at 16:14

1 Answers1

1

APIs are and should be sessionless/stateless. They can not handle it.

That's why token based authentications are used. In order to make you sure about it. Write a route in web.php file and hit it from postman or browser then you will see a session info if any.

Afraz Ahmad
  • 5,193
  • 28
  • 38
  • I know APIs should be stateless, but I need to save a session in my API project because from API, I manage PayPal payments, so when PayPal redirects back to my website after payment, I need access to the shopping cart I saved in the session. – Sergio Aug 18 '18 at 16:36