简体   繁体   English

Session laravel 8 + 惯性 js 超时

[英]Session timeout in laravel 8 + inertia js

Am using laravel 8 and inertia js and I set session_timeout to 10 minutes in.env file.我正在使用 laravel 8 和惯性 js,我将 session_timeout 设置为 10 分钟 in.env 文件。

The problem is after 10 minutes of inactivity when I submit a form i get a model saying 419 |问题是在我提交表单后 10 分钟不活动后,我得到一个 model 说 419 | PAGE EXPIRTED.页已过期。 And if i try to visit other pages it works like there is no timeout session如果我尝试访问其他页面,它就像没有超时 session

Here is the code of my.env这是 my.env 的代码

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

code in session.php session.php 中的代码

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

    'expire_on_close' => true,

在此处输入图像描述

How can I redirect user to login page after 10 minutes of inactivity?如何在 10 分钟不活动后将用户重定向到登录页面?

My handler.php file我的 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()
    {
        //
    }
}

Go to you App\Exceptions\Handler file, and in the render method you could take some action, like this: Go 到你的App\Exceptions\Handler文件,在渲染方法中你可以采取一些行动,像这样:

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 */

}

I was able to log user out after 10 minutes of inactivity using middleware.在使用中间件 10 分钟不活动后,我能够将用户注销。

You can find the code here:你可以在这里找到代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM