required to be logged in middleware laravel code example

Example 1: artisan make auth

composer require laravel/ui
php artisan ui vue --auth

Example 2: create new authentication middleware laravel

php artisan make:middleware BasicAuth //In console.

//BasicAuth.php file is created:
admin == 1);
  
  	//This will be excecuted if the new authentication fails.
	if (! $isAuthenticatedAdmin)
		return redirect('/login')->with('message', 'Authentication Error.');
	return $next($request);
}

//In app/Http/Kernel.php, add this line:
protected $routeMiddleware = [
	/*
	* All the laravel-defined authentication methods
	*/
  'adminAuth' => \App\Http\Middleware\AdminAuth::class //Registering New Middleware
];

//In routes/web.php, add this at the end of the desired routes:
Route::get('/adminsite', function () {
	return view('adminsite');
})->middleware('adminAuth'); //This line makes the route use your new authentication middleware.

Tags:

Misc Example