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

masterpowers's avatar

Laravel Database Session Causes Token Mismatch?

My App Condition is like this

I got Multi-Auth Enable plus Multi Database Connection

My session driver is app.database (simple Database)

with a Model Session

Im Using User Model Out of the Box

Plus Create a Admin Model Configure for Other Db Connection

To put this Simply When i Change my session driver to file in config/session.php

I receive no error since im using the file mode

but when i use Database Session with Multi-Auth

I get VerifyCsrf Token at line 53

public function handle($request, Closure $next)
    {
        if ($this->isReading($request) || $this->shouldPassThrough($request) || $this->tokensMatch($request)) {
            return $this->addCookieToResponse($request, $next($request));
        }

        throw new TokenMismatchException; // THIS IS THE CAUSE OF ERROR
    }

In line to this , this is my SessionServiceProvider.php where you can see im using app.database

Which is suppose to be file in the config/session.php

public function register()
    {
        $this->app->session->extend('app.database', function ($app) {
            $connectionName     = $this->app->config->get('session.connection');
            $databaseConnection = $app->app->db->connection($connectionName);

            $table = $databaseConnection->getTablePrefix() . $app['config']['session.table'];

            return new DatabaseSessionHandler($databaseConnection, $table);
        });
    }

From There If There is an Authenticated User it will Write in my database sessions table

public function write($sessionId, $data)
    {
        $type = \Auth::currentType();

        $user_id = (\Auth::type($type)->check()) ? \Auth::user()->id : null;

        if ($this->exists) {
            $this->getQuery()->where('id', $sessionId)->update([
                'payload' => base64_encode($data), 'last_activity' => time(), 'user_id' => $user_id, 'user_type' => $type,
            ]);
        } else {
            $this->getQuery()->insert([
                'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(), 'user_id' => $user_id, 'user_type' => $type,
            ]);
        }

        $this->exists = true;
    }

Im a Getting a Session When I Log In a User Even if I Receive Token Mismatch

I Verified it Coz i Went to php artisan tinker

$seesion->all() it shows me a session which supposed to be

having a session but no user_id or type if it is not authenticated

I Dont Really Know What is Lacking in My Code

Only Solution that Comes into My Mind is Disable VerifyCsrfToken.php in the Kernel.php as part of the Middleware.

Hope Someone can Clarify things out to me.

Is Database Session Really Causing this Token Mismatch?

Coz ive already done everything i can search on the web clear cache etc.

Hope You Can Help Out Clear my Mind thanks

0 likes
2 replies
alejometal's avatar

Hi, Friends, Check on file driver the size of each session file, and check the datatype of payload field on session table, that is by default TEXT which in mysql are 64kb, if the session files are greater than that, try to change datatype of payload field to MEDIUMTEXT or something more fit.

Please or to participate in this conversation.