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

aavinseth's avatar

csrf not generating on custom session create

I have created a custom session to handle sessions of admin, company, etc and on login with company below are the scenerio,

  1. with SwitchSessionDriver middleware enable, I am getting null in csrf_token() and session is customsession (need this enable and data in csrf token)
  2. with SwitchSessionDriver middleware disabled, I am getting data in csrf_token() and session is database

Company Login

if(Auth::guard('company')->attempt($request->only('email','password'),$request->filled('remember'))) {
            return redirect()->intended(route('company.home'))->with('status','You are Logged in as Company!');
        }

AppServiceProvider.php

Session::extend('customsession', function (Application $app) {
            return new CustomSessionHandler(DB::connection(), 'sessions', config('session.lifetime'), $app);
        });

Kernal.php

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\SwitchSessionDriver::class,
        ],

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

SwitchSessionDriver.php

CustomSessionHandler.php

0 likes
2 replies
aavinseth's avatar

@malikmeraj csrf_token() is working for database session but with custom session it is showing blank

Please or to participate in this conversation.