我正在使用 Laravel Breeze。我的應用程式將有兩個訪問站點:
- 管理員可以登錄與用戶表相關的(作業)
- 客戶可以登錄相關的客戶表(待討論)
我不知道,如果這是一個好主意,但我想與另一個模型共享已經實作的身份驗證服務:客戶端。
我已經實作的是:
- 登錄用戶可以創建客戶端
- 客戶收到來自活動的電子郵件驗證電子郵件
- 客戶可以確認電子郵件地址并將被重定向到登錄視圖
現在對我來說變得很棘手。該AuthenticatedSessionController::store()方法正在檢查$request->authenticate()并且這將再次運行用戶表并且對于客戶端表失敗。我根據登錄候選者的型別稍微修改了重定向方法:
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
$user = User::where('id', Auth::id())->first();
if ( $user ) {
return redirect()->intended(RouteServiceProvider::HOME);
}
$client = Client::where('id', Auth::id())->findOrFail();
if ( $client ) {
return redirect()->to('portal.show', $client->id);
}
}
但這僅適用于用戶,因為$request->authenticate()不會讓客戶進入。現在我需要支持,從這里開始,我只是猜測下一步是我所做的更新config/auth.php:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'clients' => [
'provider' => 'clients',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
uj5u.com熱心網友回復:
您將需要查看和修改類authenticate的方法App\Http\Requests\Auth\LoginRequest。
一種方法是檢查是否存在email通過請求users和clients表格接收到的資訊。
然后,如果電子郵件屬于客戶端 - 實作自定義邏輯以根據clients表記錄驗證憑據并登錄客戶端
如果電子郵件屬于用戶 - 然后讓請求正常通過
例如:
public function authenticate()
{
$type = DB::table('clients')->where('email', $this->email)->exists() ? 'client' : 'user';
if($type === 'user') {
return $this->authenticateUser();
}
//Validate credentials against `clients` table
// If valid retrieve the client and login the client
//Else throw ValidationException
}
//Copy the content of original authenticate to this method
protected function authenticateUser()
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
注意使用這種方法,您不會進行任何速率限制/節流,也不會觸發嘗試事件
對于客戶端完整的健壯身份驗證系統,我想需要定義一個單獨的保護,用戶提供程式,然后重新實作SessionGuard功能。
另一個瘋狂的想法是,為什么不將Client表示為users表上的記錄只是為了進行身份驗證,并且它們之間可能存在一對一的關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485541.html
