how Use Laravel Auth middleware

 

If you want set any middleware in larave you need to change kernel.php file which is located in app/http folder
In Kernel.php – you can set middleware like this:

/**
* The application’s route middleware.
*
* @var array
*/
protected $routeMiddleware = [
‘auth’ => ‘App\Http\Middleware\Authenticate’,
‘auth.basic’ => ‘Illuminate\Auth\Middleware\AuthenticateWithBasicAuth’,
‘guest’ => ‘App\Http\Middleware\RedirectIfAuthenticated’,

‘admin’ => \App\Http\Middleware\Admin::class,
‘guestadmin’ => ‘App\Http\Middleware\RedirectIfAuthenticated’,
];

If you want to create Admin middleware you need to create Admin.php in middleware folder which in located in app folder.
Create admin and copy code below:-
return redirect()->route(‘admin.login’);
}

return $next($request);
}

}

Define ROutes in route file
/*
|————————————————————————–
| Application Routes for BackEnd
|————————————————————————–
*/
Route::group([‘namespace’ => ‘BackEnd’, ‘prefix’ => ‘admin’], function() {
#Access only guest users
Route::group([‘middleware’ => [‘adminguest’]], function(){
#Signin
Route::get(‘/login’, [
‘as’ => ‘admin.login’,
‘uses’ => ‘AdminController@getSignin’
]);
Route::post(‘/login’, [
‘as’ => ‘admin.login’,
‘uses’ => ‘AdminController@postSignin’
]);
Route::post(‘/logout’, [
‘as’ => ‘admin.logout’,
‘uses’ => ‘AdminController@getLogout’
]);
});

#Access only login users
Route::group([‘middleware’ => [‘admin’]], function(){
#index
Route::get(‘/’, [
‘as’ => ‘admin.index’, function () {
$title = “Dashboard”;
return view(‘BackEnd.dashboard’, compact(‘title’));
}
]);

});

});

Now you are able to acess http://example.com/admin

Write your login code in Admin Controller