How to logout user through specified time in laravel

1.4k views Asked by At

How can I logout a user after 48 hours.

public function generatePass(Request $request)
{
    $myRandomString = Str::random(8);
    
    DB::table('web_passes')->insert([
        'pass' => $myRandomString,
        'user_id' => $request->user_id,
        'expiry_time' =>   Carbon::now()->addDays(2)
    ]);
2

There are 2 answers

0
Ossama Abd On

you can change the SESSION_LIFETIME=20 in .env

the life time of session in this example is 20 minutes

so in any request should be authorize give it

if(auth()->check()) complete execute

else

remove or refersh token to invalid and redirect to login page

0
mqamar On

I just did that by simply applying a middleware condition

 public function handle(Request $request, Closure $next)
    {
        $webPass = $request->user()->webPass;
        if($webPass->expiry_time < Carbon::now()){
            $randomString = str::random(8);
            $webPass->pass = $randomString;
            $webPass->expiry_time = Carbon::now()->addSeconds(30);
            $webPass->update();
            Auth::logout();
            return redirect('/otp')->with('expiry_time', 'Your session has expired');
        }
        return $next($request);
    }