繁体   English   中英

Laravel会话超时后如何实现锁屏

[英]Laravel how to achieve after the session timeout lock screen

我尝试了多种Internet途径,但无法实现,希望有人能给我一种实现思想或方法的途径,谢谢您的帮助。

假设您使用会话驱动程序来处理身份验证,则可以更改空闲会话过期的时间,

/app/config/session.php文件。

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/

'lifetime' => 120,    // minutes

'expire_on_close' => false,

让我提供一个例子。 app\\Http\\Middleware文件夹中定义一个SessionTimeout中间件。

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use Session;

class SessionTimeout
{


     /**
     * Check the incoming request for session data, log out if session lifetime is exceeded.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

     public function handle($request, Closure $next)
     {

        //$isLoggedIn = $request->path() != '/logout';

        $bag = Session::getMetadataBag();

        $max = $this->getTimeOut();

        if (($bag && $max < (time() - $bag->getLastUsed()))) {

            //$cookie = cookie('intend', $isLoggedIn ? url()->current() : 'auth/login');

            $email = Auth::user()->email;

            $returnPath = url()->current();

            $request->session()->flush(); // remove all the session data

            Auth::logout(); // logout user

            return redirect('auth/login')
                    ->withInput(compact('email', 'returnPath'))
                    //->withCookie($cookie)
                    ->withErrors(['Please login']);
            //you could also redirect to lock-screen, a completely different view 
            //and then pass the returnPath to controller method maybe via hidden filed
            //to redirect to the last page/path the user was on 
            //after successful re-login from the lock-screen.
        }

        return $next($request);


     }

     /**
     * Set a variable in .env file TIMEOUT (in seconds) to play around in the development machine.
     */
     protected function getTimeOut()
     {
        return (env('TIMEOUT')) ?: (config('session.lifetime') * 60);
     }
}  

SessionTimeout添加到app\\Http\\Kernel.php

class Kernel extends HttpKernel {
 /**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
 protected $middleware = [
      'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
      'Illuminate\Cookie\Middleware\EncryptCookies',
      'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
      'Illuminate\Session\Middleware\StartSession',
      'Illuminate\View\Middleware\ShareErrorsFromSession',
      'App\Http\Middleware\SessionTimeout'
 ];
 /**
 * The application's route middleware.
 *
 * @var array
 */
 protected $routeMiddleware = [
      'auth' => 'App\Http\Middleware\Authenticate',
      'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
      'guest' => 'App\Http\Middleware\RedirectIfAuthenticated'
 ];

}

然后,在登录表单的视图中,通常在resources\\views\\auth\\login.blade.php

@extend('app-layout')
@section('content')
    //code to display errors here

    @if($email) //check if the request has $email returned by SessionTimeout middleware
        //if so display lock screen like
        //code to display the profile image
        //code to display the user email (or whatever id is used)
    @else
        //display email input field for a new login
        //code to input the email (whatever id is used) for a new login
    @endif
    //here the code common for lock screen as well as new login.
    //code to display input password 
    //code for submit button and rest of the things like remember me field
@stop  

您也可以使用@if($email)来锁定屏幕和新的登录表单,并基于@if($email)

希望这会帮助您入门。

暂无
暂无

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

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