Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel middleware check if user is logged in

//Create the middleware if not exists
class RedirectIfAuthenticated 
{ 
    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
              	//redirect to where you want
                return redirect(url('dashboard'));
            }
        }
        return $next($request);
    }
}


//Then use the middleware in pages you don't want logged in users to access like this
Route::get('/register', [AuthController::class, 'register'])->name("register")
    ->middleware(RedirectIfAuthenticated::class);

Comment

how to check if a user is logged in in a non middleware controller in laravel

How to check if a user is logged in, in a non middleware controller
  in laravel?
  
  auth Auth::guard
    
    if (Auth::guard('api')->check()){ //will return true if logged in
      //do this 
    }
  else //if the user is not logged in
  {
    //do this
  }
Comment

PREVIOUS NEXT
Code Example
Php :: carbon date minus days 
Php :: sentence get first second word php laravel 
Php :: overwrite file php 
Php :: laravel display old value in form 
Php :: run python script from batch file with arguments 
Php :: laravel migration change column length 
Php :: datediff in hour query builder laravel 
Php :: php current date 
Php :: laravel validation array 
Php :: concat() function using laravel eloquent query 
Php :: composer cache clean 
Php :: mac os change the php verison 
Php :: confirm password validation in laravel 
Php :: php keep only letters and numbers 
Php :: Add 5 days to the current date in PHP 
Php :: get category post in wordpress 
Php :: joomla login link 
Php :: php number positive 
Php :: add custom user meta and display it in user page 
Php :: php carbon from timestamp 
Php :: php echo alot of html 
Php :: call to a member function connection() on null test laravel 
Php :: table drop foreign php laravel 
Php :: how login with phone in laravel 
Php :: php empty 
Php :: update pdo mysql php 
Php :: page expire in laravel 
Php :: how to publish stubs in laravel 
Php :: multiply a string php 
Php :: wordpress hook add javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =