我有两个守护者“客户”和“管理员用户”。在“管理员登录”登录页面中,验证规则在logincontroller中不起作用。我有面向管理员用户的checkRole中间件。为什么这不起作用。验证是由该checkRole中间件完成的?与客户登录相同的问题。无需输入任何登录详细信息即可访问adminhome(管理仪表板页面)。怎么回事我该如何解决。
这是我的路线
`Auth::routes();
Route::get('/index', 'FrontController@index')->name('index');
Route::get('/admin/login', 'adminloginController@ShowAdminLoginForm')->name('adminLogin');
//Customer Register Route
Route::post('/register', 'FrontController@storeRegister');
//Login Routes-Post Data
Route::group(['middleware' => 'guest:customer'], function () {
Route::post('/customerLogin', 'Auth\LoginController@customerLogin');
});
Route::group(['middleware' => 'roles:adminuser'], function () {
Route::post('/adminUserLogin', 'Auth\LoginController@AdminLogin');
});
Route::view('/adminhome', 'home');
Route::view('/customerhome', 'index');
Route::get('/adminlogout', 'Auth\LoginController@adminlogout');
Route::get('/customerlogout', 'Auth\LoginController@customerlogout');`
这是logincontroller.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->middleware('roles')->except('logout');
}
/**
* Admin Login
*/
public function AdminLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('adminuser')->attempt([
'email' => $request->username,
'password' => $request->password,
]);
if ($status) {
//dd($status);
return redirect()->intended('/adminhome');
//return redirect()->intended('/adminhome');
//$request->session()->flash('success', 'Login Successfully!');
// return redirect('/adminhome'); // redirect to admin dashboard
}
//$request->session()->flash('error', 'Sorry,Login Unuccessfull!');
return redirect()->back();
//return back()->withInput($request->only('email', 'remember'));
}
/*public function getUserId()
{
$admin_user_id = Auth::guard($guard)->user()->id;
return $admin_user_id;
}*/
//login for the customer
public function customerLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('customer')->attempt([
'email' => $request->username,
'password' => $request->password],
$request->get('remember'));
//dd($status);
if ($status) {
return redirect()->intended('/customerhome');
//$request->session()->flash('success', 'Login Successfully!');
//return redirect()->intended('/index');
}
//$request->session()->flash('error', 'Sorry,Login Unuccessfull!');
return redirect()->intended('/customerhome');
//return back()->withInput($request->only('email', 'remember'));
}
/*public function getUserid()
{
$id = Auth::guard($guard)->user()->id;
return $id;
}*/
public function adminlogout(Request $request)
{
//$request->
Auth::guard('adminuser')->logout();
//$request->
session()->flush();
return redirect('/admin/login');
}
public function customerlogout(Request $request)
{
Auth::guard('customer')->logout();
return redirect('/index');
}
}
checkrole中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Models\AdminUser;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($guard === "adminuser" && Auth::guard($guard)->check()) {
return redirect('/adminhome');
}
if (!Auth::guard($guard)->check()) {
return redirect('/admin/login');
}
return $next($request);
}
}
内置用于客户登录的RedirectifAuthenticated中间件
public function handle($request, Closure $next, $guard = null)
{
//dd($guard);
if ($guard === "customer" && Auth::guard($guard)->check()) {
return redirect('/customerhome');
}
if (!Auth::guard($guard)->check()) {
return redirect('/index');
}
return $next($request);
}
}