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...
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.
Solved by setting the session_driver to database (wich is a good idea in prod anyway)
Followed this post: Laravel automatically logged out after few seconds?