问题描述
我正在尝试进行多重身份验证,但我似乎无法使其正常工作。我使用的是自定义中间件而不是 RedirectIfAuthenticated 中间件,但 guard 总是在我的中间件中返回 null,但是当我使用 RedirectIfAuthenticated 中间件时,它可以工作。 这是我的代码
Auth.PHP
from elasticsearch import Elasticsearch,Requestshttpconnection
from elasticsearch_dsl import Search
# use aws4auth to make auth
# host is without https://
es = Elasticsearch(hosts=[{'host': host,'port': 443}],http_auth=auth,use_ssl=True,verify_certs=True,connection_class=Requestshttpconnection)
s = Search.from_dict({
"query": {
"regexp": {
"roadmapParams.L.M.project_id.S": partial_match
}
},}).index('MYINDEX').using(es)
response = s.execute()
用户模型
'defaults' => [
'guard' => 'web','passwords' => 'users',],/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next,you may define every authentication guard for your application.
| Of course,a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session","token"
|
*/
'guards' => [
'web' => [
'driver' => 'session','provider' => 'users','api' => [
'driver' => 'token','hash' => false,'admin' => [
'driver' => 'session','provider' => 'admins','user' => [
'driver' => 'session','provider' => 'users'
],/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database","eloquent"
|
*/
'providers' => [
'admins' => [
'driver' => 'eloquent','model' => App\Admin::class,'users' => [
'driver' => 'eloquent','model' => App\User::class,// 'users' => [
// 'driver' => 'database',// 'table' => 'users',// ],
}
userLoginController
public $table = 'users';
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guard = 'user';
protected $fillable = [
'name','email','password',];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password','remember_token',];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',];
public function verifyUser() {
return $this->hasOne('App\VerifyUser');
}
}
这是我的中间件
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest:user')->except('logout');
}
public function showLoginForm() {
return view('user.login');
}
protected function guard() {
return Auth::guard('user');
}
public function login(Request $request) {
$this->validate($request,[
'email' => 'required|email','password' => 'required|min:8'
]);
if(Auth::guard('user')->attempt(['email' => $request->email,'password' => $request->password],$request->remember)) {
// return $this->sendLoginResponse($request);
// if(Auth::check()) {
return redirect()->route('userdashboard');
// }
}
return redirect()->back()->withInput($request->only('email','remember'));
}
protected function authenticated(Request $request,$user) {
echo $user->email_verified_at;
die();
// if($user->email_verified_at == null) {
// Auth::logout();
// return back()->with('warning','You need to confirm your email account. We have sent you an email verification code. Please check your email.');
// }
}
public function logout() {
session::flush();
Auth::logout();
return redirect()->route('userlogin');
}
protected function loggedOut(Request $request)
{
return redirect('user/login');
}
解决方法
如果你想让你的中间件得到一个参数,你必须在分配中间件时用参数来定义它:
Route::get(...)->middleware('user:user');
第一个 user
是中间件的名称。 :
之后是将传递给中间件的 handle
方法的参数。所以这个中间件最终会以 $guard
为 user
。