我正在关注登录教程。
我逐步编写了所有代码,但最后我无法登录,即使我提供了正确的凭据。
以下是我的代码:
的LoginController
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt(['username'=>$request->username,
'password'=>$request->password,'active'=> 1]);
if($auth)
{
return redirect()->route('dashboard');
}
return redirect()->back()->with(['error' =>'Wrong Credentials']);
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
DashboardController
public function __construct()
{
$this->middleware('auth');
}
public function dashboard()
{
return view('layouts.app');
}code here
中间件(VisitorsMiddleware.php)
public function handle($request, Closure $next, $guard = 'web')
{
if(!Auth::guard($guard)->check()){
return redirect()->route('dashboard');
}
return $next($request);
}
CheckRole.php
epublic function handle($request, Closure $next)
{
$roles = $this->getRequiredRoleForRoute($request->route());
if($request->user()->hasRole($roles)|| !$roles)
{
return $next($request);
}
return redirect()->route('/noPermission');
}
private function getRequiredRoleForRoute($route)
{
$actions = $route->getAction();
return isset($actions['roles']) ? $actions['roles']: null;
}
web.php
Route::group(['middleware'=>['visitors','roles']],function (){
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);});
Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@postLogin']);