简体   繁体   中英

Laravel 5.4 return to login page after auth session expire

hi :) i develop some project and i have the same problem in all of them , i cannot handle the method when we did not use the web application for example in 1 hour after refresh the page login page appear instead of error page , please help me how can redirect to login page after the auth timeout., i use this code in beginning of some methods to get user id and when the auth timeout this part of code has error and the error page appear :

$user = Auth::user();
$user_id = $user->id;

and the error is :

(1/1) ErrorException
Trying to get property of non-object 

i want to redirect to login page instead of this error page, thanks a lot :)

Check if user is logged in before getting user model:

       if(Auth::check()){
           $user = Auth::user();
           $user_id = $user->id;
       }
       else{
           return redirect('login');
       }

You can check user logged in or not before using that like

if ( Auth::check() ) {
    $user = Auth::user();
    $user_id = $user->id;
}else{
    return redirect('login');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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