正如标题所说,在VendorController
postLogin
方法dd(Auth::user())
中获取值为null,但$attempt
获取结果true
,我可以&# 39;获取已尝试的数据。
我的routes/web.php
Route::group(['prefix' => 'vendor','as' => 'Vendor.'], function () {
Route::get('login', 'VendorController@login')->name('Login');
Route::post('login', 'VendorController@postLogin')->name('Post.Login');
});
我的VendorController
:
public function postLogin(Request $request)
{
$attempt = Auth::guard('vendor')->attempt([
'acctId' => $request->acctId,
'password' => $request->password
]);
if($attempt){
dd(Auth::user());
}
}
我的Vendor
模型
namespace ACME\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class Vendor extends Model implements Authenticatable
{
use AuthenticableTrait;
protected $table = 'vendor';
protected $primaryKey = 'id';
public $timestamps = true;
protected $fillable = [
'acctId',
'password',
'active',
'remark'
];
protected $guarded = ['password'];
}
我的config/auth.php
return [
'defaults' => [
// 'guard' => 'web',
// 'passwords' => 'users',
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
// 'web' => [
// 'driver' => 'session',
// 'provider' => 'users',
// ],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'vendor' => [
'driver' => 'session',
'provider' => 'vendors',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => ACME\Entities\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => ACME\Entities\Admin::class,
],
'vendors' => [
'driver' => 'eloquent',
'model' => ACME\Entities\Vendor::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'user',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'admin',
'expire' => 60,
],
'vendors' => [
'provider' => 'vendors',
'email' => 'auth.emails.password',
'table' => 'vendor',
'expire' => 60,
],
],
];
答案 0 :(得分:4)
因为您使用的是自定义驱动程序而您没有使用Auth::shouldUse()
来定义该方法需要使用的驱动程序,所以您需要像这样调用经过身份验证的用户:
Auth::guard('vendor')->user()
在另一个解决方案中,您可以使用Auth::shouldUse()
方法。
public function postLogin(Request $request)
{
Auth::shouldUse('vendor');
if(! $attempt = Auth::attempt([
'acctId' => $request->acctId,
'password' => $request->password
])){
//handle error if there is not login
}
dd(Auth::user());
}
通过执行此操作,您可以将驱动程序设置为使用供应商。