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

Modi.Bade's avatar

Managing users sessions by laravel authentication mechanism

hello,

first of all im new in Laravel and i used Codeigniter before...

Users in my project have one session for each system they logged in to and each session has its own token.

sessions are stored in sessions table and sessions table is related to users table for retrieving the users informations by token

User token and authentication is written by me and I do not use Laravel authentication system because I recently transferred my project from Codeigniter to Laravel.

so how can i manage this sessions and token generation with laravel authenticating system ?

0 likes
16 replies
Modi.Bade's avatar

how laravel fortify makes tokens ? is it for web applications or i can use it for my api

1 like
Emokores's avatar

@jordi.el.nino.pola You can use any of these for token generation:

  1. Tokens can be any set of characters you choose. Then you can use str_shuffle($string) in PHP to generate the token.

  2. You can use PHP's uniqid() function to get randomized characters for the token.

Then you can validate the token on the user request

2 likes
Modi.Bade's avatar

@Emokores im doing this right now.

im generating token by my self and send it to the front end and validate it by custom middleware before every routes.

my goal for using laravel authenticating system is that i want to use broadcast private channels with laravel-echo-server and authenticating private channels needs laravel auth system

actually i'm little confused about it

1 like
Emokores's avatar

@Jordi.El.Nino.Pola Oh okay! Then Laravel's authentication system handles everything for you. Read the docs on authentication sessions and authorization. Everything is well laid out. You might not even need your sessions migration, depending. See how you can do without it if it's not needed

1 like
Emokores's avatar

@Jordi.El.Nino.Pola You only need Laravel Sanctum if you're accessing the backend through an API, especially if your application is an SPA (JavaScript-rendered frontend). Use Fortify if you're not accessing the backend through an API

1 like
Modi.Bade's avatar

@Emokores yes i'm accessing the backend through an API, my app is spa ( reactJs ).

as i said at the first my users have sessions and token's are stored in sessions table per user session.

so can i handle this with sanctum ?

Emokores's avatar
Emokores
Best Answer
Level 2

@Jordi.El.Nino.Pola Very well! You can use Sanctum. It will help to setup the session management, authentication and authorization sequences. Read the docs for more details

1 like

Please or to participate in this conversation.