gpt4 book ai didi

php - Laravel 5.3 尝试为真但 Auth::User 返回 null?

转载 作者:行者123 更新时间:2023-12-01 22:20:00 25 4
gpt4 key购买 nike

正如标题所说,在VendorController postLogin 方法dd(Auth::user()) 中获取了值null,但是$attempt 得到结果true,我无法获取已经尝试过的数据。

我的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());
}
}

我的供应商模型

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,
],
],

];

最佳答案

因为您正在使用自定义驱动程序并且您没有使用 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());
}

通过这样做,您可以将驱动程序设置为使用供应商。

关于php - Laravel 5.3 尝试为真但 Auth::User 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com