为什么auth中间件工作一半时间,有惯性/laravel

问题描述 投票:0回答:1

我一直在尝试使用 digitalOcean 应用程序平台部署 Laravel / Inertia 应用程序。在开发环境中一切顺利。在生产中,中间件、身份验证和我制作的自定义中间件只有一半的时间可以工作。

有时单击链接会将您带到页面,有时中间件会重定向到登录。刷新登录页面,这次就没有拦截了。

这两个中间件都会发生这种情况,即使用户已连接,也会启动。清爽解决了这个问题。我正在使用微风惯性脚手架。 auth 中间件是它附带的。

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');
    }
}

这是我的路线:

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');

Eromaps 页面示例:


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>
    );
}

我刚刚结束了 10 个月的代码学习训练,我已经不知所措了......我姐姐在这方面信任我,所以我正在尽力而为。到目前为止,我一直在调试我自己不明白的这一点。任何帮助将不胜感激...

我尝试在控制器中进行身份验证检查,以便在 Auth()->检查失败时返回登录页面。 结果相同。

Auth()->user 的 dd() 有时会给我它的 id,有时会尝试读取 null 上的属性“id”。刷新页面会随机给我一个或另一个。

laravel authentication inertiajs laravel-middleware
1个回答
0
投票

通过将 session_driver 设置为数据库来解决(无论如何,这在产品中是一个好主意)

关注了这篇文章: Laravel 几秒钟后自动退出?

© www.soinside.com 2019 - 2024. All rights reserved.