donglan6967 2015-03-17 00:11
浏览 103

Laravel 5 - 检查用户会话是否已过期

I am trying to implement a way to check if the user session has expired and then pop-up a modal letting the user know.

Here is what I have so far.

Route

Route::get('auth-check', ['uses' => 'UsersController@authCheck', 'as' => 'auth.check']);

UsersController

use Illuminate\Contracts\Auth\Guard;

class UsersController extends Controller {

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

...

    /*
    * returns {status: false} or {status: true}
    */
    public function authCheck()
    {
        return response(['status' => $this->auth->check()])
                      ->header('Content-Type', 'application/json');
    }

}

Master blade (so it can be checked on any page)

<!-- Modal -->
<div class="modal fade" id="session-expired">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Session Expired!</h4>
            </div>
            <div class="modal-body">
                <p>Your session has expired, please login to continue.</p>
            </div>
            <div class="modal-footer">
                <a href="{!! route('login') !!}" class="btn btn-danger">Login</a>
            </div>
        </div>
    </div>
</div>

JS

@if(Auth::check())
$.fn.timer = function()
{
    $.get("{{ route('auth.check') }}", function (data) {
        data.status == false ? $('#session-expired').modal({ show: true, backdrop: 'static' }) : $('#session-expired').modal('hide');
    });
};

window.setInterval(function(){
    $('#timedOutSession').timer();
},10000);
@endif

It kinda works, when I am logged in it returns {status: true}. But after 5-10 requests on checking the auth user the session dies for some reason and it returns {status: false}. Like making so many auth checks kills the session as a security measure or something.

Anyone mess around with letting a user know if their session has expired? Reason being is the application is very form based and users may leave a form page open and come back in the next day and the session has expired but they will go through the process of filling out the lengthy form and submit it just to find out they are not logged in.

Any ideas or better ways to do this would be quite welcome.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
    • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题