【问题标题】:Session timeout in laravel 8 + inertia jslaravel 8 +惯性js中的会话超时
【发布时间】:2021-08-23 08:48:21
【问题描述】:

我正在使用 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()
    {
        //
    }
}

【问题讨论】:

    标签: php laravel vue.js session inertiajs


    【解决方案1】:

    转到你的 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 */
    
    }
    

    【讨论】:

    • 这不起作用 + 当我制作缓存时它会产生错误:清除``` PHP 致命错误:无法检查 App\Exceptions\Handler::render($request, App\Exceptions \Throwable $e)```
    • 忘记清理缓存,一会儿,重定向成功了吗?
    • 由于错误,我什至无法运行 php artisan serve
    • 好吧,我需要看看 2 件事才能帮助你,你原来的 Handler 文件,以及当前抛出的错误日志。
    • 我修复了 php artisan serve 后抛出的异常,结果我没有导入 throwable 类,但现在我得到一个空白模式而不是 419 错误
    【解决方案2】:

    我能够在使用中间件 10 分钟不活动后注销用户。

    你可以在这里找到代码:

    https://alfrednutile.info/posts/168/

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2015-02-24
      • 2022-11-04
      • 2018-07-22
      • 1970-01-01
      • 2021-05-30
      • 2013-01-19
      相关资源
      最近更新 更多