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

velwitch's avatar

Laravel / Inertia auth middleware going crazy

Hello, I've been trying to deploy a Laravel / Inertia app, using digitalOcean app Platform. In dev environment everythings goes well. In production, the middleware, auth and a custum one I made, works only half the time.

Sometime clicking the link will send you to the pages, sometimes the middleware will redirect to the login. Refresh the login page and this time is goes without interception.

It happens with both middlewares, that will kick-in even though the user is connected. And refreshing solves it. I'm using the breeze inertia scafolding. The auth middleware is the one that comes with it.

class isActive extends Middleware
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $userId = Auth::user()->id;
            $user = User::find($userId);
            if ($user->isActive == 'yes') {
                return $next($request);
            }
        }

        return redirect('/offer');
    }
}

Here are my routes :

Route::middleware(['auth','isActive'])->group(function () {
    Route::get('/eromaps', [PagesController::class, 'eromaps'])->name('eromaps');

    Route::get('/eromaps/desir', [DesirController::class, 'desir'])->name('desir');
    Route::get('/eromaps/desir/profile', [DesirController::class, 'profile'])->name('desirProfile');

    Route::get('/eromaps/plaisir', [PlaisirController::class, 'plaisir'])->name('plaisir');   
    Route::get('/eromaps/plaisir/profile', [PlaisirController::class, 'profile'])->name('plaisirProfile');

    Route::get('/eromaps/union', [UnionController::class, 'union'])->name('union');  
    Route::get('/eromaps/union/profile', [UnionController::class, 'profile'])->name('unionProfile');

Exemple of the Eromaps page:


import { Link, Head } from "@inertiajs/react";
import NavBar from "../Components/NavBar";
import Footer from "@/Components/Footer";

export default function Eromaps() {
    return (
        <main className="flex flex-col items-center min-w-screen min-h-screen bg-[url('/storage/images/map1-op.jpg')] bg-cover bg-center">
            <div className=" w-10/12 min-h-screen max-w-7xl flex flex-col items-center justify-center md:justify-between relative">
                <NavBar/>
                <div></div>
                <div>
                <h1 className="mt-32 md:mt-0 text-center text-4xl md:text-5xl font-rock">Choisis une Eromap !</h1>
                <div className="flex flex-col justify-center items-center gap-8 mt-12 md:grid md:grid-cols-2 md:mt-14 md:gap-x-16 md:gap-y-10">
                    <Link href={route("desir")}>
                        <img
                            className="rounded-xl w-72 shadow-xl shadow-gray-400  hover:shadow-pink-300 hover:border-2 hover:border-pink-300"
                            src="/storage/images/desir.png"
                            alt=""
                        />
                    </Link>
                    <Link href={route("plaisir")}>
                        <img
                            className="rounded-xl w-72 shadow-xl shadow-gray-400  hover:shadow-pink-300 hover:border-2 hover:border-pink-300"
                            src="/storage/images/plaisir.png"
                            alt=""
                        />
                    </Link>
                    <Link href={route("union")}>
                        <img
                            className="rounded-xl w-72 shadow-xl shadow-gray-400  hover:shadow-blue-200 hover:border-2 hover:border-teal-300"
                            src="/storage/images/union.png"
                            alt=""
                        />
                    </Link >
                    <Link href={route("eveil")}>
                        <img
                            className="rounded-xl w-72 shadow-xl shadow-gray-400  hover:shadow-blue-200 hover:border-2 hover:border-teal-300"
                            src="/storage/images/eveil.png"
                            alt=""
                        />
                    </Link>
                </div>
                </div>
                <div className="mt-10">
                    <Footer />
                </div>
            </div>
        </main>
    );
}

I've barely came out of an 10 month code learning formation, I'm in over my head... My sister trusted me with this, so i'm doing my best. So far I've been debbugging all myself be this one I don't get. Any help would be sincerely appreciated...

0 likes
6 replies
velwitch's avatar

I tried to put an auth check in the controller instead, to return the login page when Auth()->check fails. Same result.

A dd() of Auth()->user gives me sometime its id, sometime a Attempt to read property "id" on null. Refreshing the page will randomly give me one or the other.

velwitch's avatar

I've realised that clicking on a link sometimes refresh the session token. When it does, I get the error of Auth()->check(). Sometimes it doesn't refresh the token. Then I get the id of the user.

i'm using <Link/> from inertia, gonna see if that's the problem

velwitch's avatar

Ok so that's clearly the problem, since it doesn't happen in dev environment. Now the question is, why does my laravel session cookie randomly refreshes...

Snapey's avatar

Not solving your problem, but your middleware is doing twice as much work as it needs to;

class isActive extends Middleware
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $userId = Auth::user()->id;
            $user = User::find($userId);
            if ($user->isActive == 'yes') {
                return $next($request);
            }
        }

        return redirect('/offer');
    }
}

could be

class isActive extends Middleware
{
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->isActive == 'yes' ) {
            return $next($request);
        }

        return redirect('/offer');
    }
}
1 like
velwitch's avatar

@Snapey Indeed. Didn't know the auth facade could use added columns of the users table. Thank you !

velwitch's avatar

Solved by setting the session_driver to database (wich is a good idea in prod anyway)

"Go to your .env file and change SESSION_DRIVER=file to SESSION_DRIVER=database. Next you will need to create a session migration: php artisan session:table. Now composer dump-autoload for good practice. Finally migrate (php artisan migrate)."

Please or to participate in this conversation.