首页 > 解决方案 > laravel 8 +惯性js中的会话超时

问题描述

我正在使用 laravel 8 和惯性 js,我在 .env 文件中将 session_timeout 设置为 10 分钟。

问题是在我提交表单 10 分钟不活动后,我得到一个模型说 419 | 页已过期。如果我尝试访问其他页面,它就像没有超时会话一样工作

这是我的 .env 的代码

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=1

session.php 中的代码

    'lifetime' => env('SESSION_LIFETIME', 120),

    'expire_on_close' => true,

在此处输入图像描述

如何在 10 分钟不活动后将用户重定向到登录页面?

我的 handler.php 文件

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

标签: phplaravelvue.jssessioninertiajs

解决方案


转到您的App\Exceptions\Handler文件,在 render 方法中您可以采取一些行动,如下所示:

public function render($request, Throwable $e)
    {
        $response = parent::render($request, $e);
        

        if (!app()->environment('local') && $response->status() == 419){
            
            //Delete the session by force
            $request->session()->flush();

            //Redirects to you login page, asuming this is your component's route
             return Inertia::render('Auth/Login')
        ]);

       /* Handling other exceptions' logic */

}

推荐阅读