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 :: run composer with specific php version 
Php :: codeigniter 3 image upload 
Php :: what is cors in laravel 
Php :: yii2 migration --fields foreign 
Php :: copy file in php 
Php :: laravel download file change name 
Php :: number text short in laravel 
Php :: php header content type json 
Php :: laravel migration longtext length 
Php :: Override the route parameter names 
Php :: show phpinfo just modules 
Php :: append single quote around variable in php string 
Php :: trova corrispondenza nella stringa php 
Php :: check if data inserted in database wordpress plugin 
Php :: php encrypt password 
Php :: php strip url of all invalid characters 
Php :: hummingbird remove caching specific page php 
Php :: rest api php 
Php :: adjacent post sort order by post title 
Php :: guzzlehttp submit form file 
Php :: laravel collection every 
Php :: wordpress change email new user template 
Php :: laravel withcount change name 
Php :: php multiplei str 
Php :: change laravel port 
Php :: php return more than one value 
Php :: blade check if variable exists 
Php :: divide page in pdf with page break using php 
Php :: parent in php 
Php :: encapsulation in php 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =