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

gurpreetindybytes's avatar

Create session with laravel API

Hi, I am new in laravel and I want to create a session when my angular app call my login API. Please help me out how I create the session with my laravel API.

0 likes
11 replies
prasadchinwal5's avatar

@gurpreetindybytes Laravel provides the following methods in which you can store the session:

  • file
  • cookie
  • database
  • memcached / redis
  • array

All this is configurable and can be found in the config/Session.php file.

Thanks.

Snapey's avatar

Really, your API should be stateless.

Have a look at Passport. Either way, you have to locally store a 'session' identifier. Either an API token or a session token.

1 like
ahsan's avatar

you can add api group in your web.php file like this:

Route::middleware(['auth'])
    ->prefix('api')
    ->namespace('Api')
    ->as('api.')
    ->group(function () {
        // you can write your routes here.
    });
2 likes
Guru5005's avatar

@gurpreetindybytes Either you can store the key in local-storage or a in cookie, so the the load will be on client side not on the server, helps you to scale the application easily. The way you are dealing is wrong , that is what i feel personally. According to your title it says create sessions , means load will be on the server , again what you what to store or dont want to use local storage? Your question is not clear

primordial's avatar

@gurpreetindybytes

" I don't want to store the token in local storage it is very bad practice."

What/who gave you that idea? I believe you want stateless authentication using tokens. Best practice.

gurpreetindybytes's avatar

Can anybody give me some example code that how I would start the session with my passport API?

gurpreetindybytes's avatar

@GURUR I have created the session with my laravel API. it is only worked when I call that API from the browser. When i call the API using of angular API is not create the session. Same happen when I try it with cookies.

Please or to participate in this conversation.