phplaravellaravel-11laravel-uilaravel-mongodb

419 Error in Laravel 11 during registration


I am encountering the following error in my new installation of Laravel 11 and Vue.js when I try to submit a form, in this case, the Register form.

419 | PAGE EXPIRED

I have checked this question and yes, I do have @csrf in my register form and

<meta name="csrf-token" content="{{ csrf_token() }}">

Steps to reproduce

  1. I installed a fresh instance of Laravel 11:
composer create-project laravel/laravel test
cd test/
  1. I added the laravel/ui package
composer require laravel/ui
  1. I added the Vue.js and Auth UI
php artisan ui vue --auth

It gave me a warning:

The [Controller.php] file already exists. Do you want to replace it? (yes/no) [yes]

I typed yes then [Enter].

  1. I installed the npm packages and initialized vite
npm install && npm run dev
  1. Iniatilized the dev server and tried to register
php artisan serve

Edit There are additional steps that were done on the same repo. We work with mongodb hence we added the MongoDB configurations as well.

  1. Installed the laravel/mongodb package
composer require mongodb/laravel-mongodb
  1. Added the configurations in config/database.php as below:
'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE', 'assets'),
    'username' => env('DB_USERNAME', ''),
    'password' => env('DB_PASSWORD', ''),
    'options' => [
        'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
    ],
],
  1. Updated .env file as shown below:
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=testdb
DB_USERNAME=
DB_PASSWORD=

I am hoping there's someone with a solution.

NB I tried to install Laravel 10 and do the same processes and it works perfectly. What has changed in Laravel 11? Any help is appreciated.

Edit Below is the contents of my bootstrap/app.php.

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

and register.blade.php

<form method="POST" action="{{ route('register') }}">
  @csrf
    <div class="row mb-3">
    // Rest of code

Solution

  • It seems like you're using the database driver as your session driver. This is the default since Laravel 11.

    I'm sure that this problem has something to do with MongoDB, but I would suggest you to use the file session driver, you can do this by setting the SESSION_DRIVER variable inside your .env file to file:

    SESSION_DRIVER=file
    

    Eitherway, in your case I don't see why you would need to use a database to store your sessions. Using filesystem is much faster and has way smaller overhead.