Search code examples
laravelauthenticationsession

Cannot authenticate in fresh installed Laravel 11 with MySQL/MariaDB


I cannot have Laravel 11 authentication working with MySQL or MariaDB.

Here's what I've done:

  1. Install Laravel with composer create-project laravel/laravel foo
  2. Create Database and edit .env accordingly (connection is ok)
  3. Install Laravel/Fortify with composer require laravel/fortify
  4. Migrate the DB with default seeder (user: [email protected]/password)
  5. Created a basic login form and a basic home view which displays user name when logged in
  6. Put a basic logout form on home page
  7. Check a million times that @csrf is present in login and logout forms (it is)

Here's what happens when I test in a browser:

  • When I try to log in with the test user, I'm not logged in, but I'm correctly redirected to the appropriate page configured in Fortify.

  • When I try to log in with the test user and the remember checkbox, I do log in. But I cannot log out, I get a 419 page expired error. That's weird. Here's weirer:

  • If I use SQLite, there is no problem: I can log in and out.

  • If I use MySQL/MariaDB and file session driver, there is no problem either: I can log in and out too.

So, here's what works or not:

Database Session driver Remember me Log in Log out
MySQL / MariaDB database no no n/a
MySQL / MariaDB database yes yes no
MySQL / MariaDB file yes / no yes yes
SQLite database yes / no yes yes
SQLite file yes / no yes yes

I have found nothing about such a problem (Laravel authentication with MySQL/MariaDB combined to session database driver) neither on SO nor in Laravel issues.

I'm running:

  • Ubuntu 23.10
  • PHP 8.3.8
  • MariaDB Ver 15.1 Distrib 10.11.8-MariaDB
  • Laravel 11.12 with Fortify 1.21.3

Solution

  • One thing I forgot to mention: I use UUID for users. Therefore, session table needs to be created accordingly:

    Schema::create('sessions', function (Blueprint $table) {
                // ...
                $table->foreignUuid('user_id')->nullable()->index();