問題描述
laravel 微風 Multi Auth ‑ 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth ‑ Admin Guard with two diffirent registration)
我使用 laravel 微風 作為身份驗證腳手架包。我想使用 laravel 保護 為兩種用戶類型的兩種不同註冊表單創建多重身份驗證 (管理員,用戶)。
我想要實現的主要思想:
我在數據庫中有兩個表,一個用於管理員,另一個用於用戶我想要的要實現的是,如果管理員選擇將帳戶註冊為管理員,它將顯示一個帶有指定管理員字段的註冊表單。之後,我想檢查用戶是否以管理員身份登錄,或者如果用戶以管理員身份登錄,則將他/她重定向到僅為管理員製作的指定儀表板。
它適用於註冊,但是無法以管理員身份登錄
參考解法
方法 1:
After 3 days effort i found a solution myself.
In the function authenticate()
in app\Http\Requests\Admin\LoginRequest.php. I have replaced Auth::attempt(...)
by Auth::guard('admin')‑>attempt(...)
public function authenticate()
{
$this‑>ensureIsNotRateLimited();
if (! Auth::guard('admin')‑>attempt($this‑>only('email', 'password'), $this‑>filled('remember'))) {
RateLimiter::hit($this‑>throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this‑>throttleKey());
}
Now it works fine for admin login and register
方法 2:
Ok, so my project is a bit different and not gives a 100% answer but I decided to leave these here, it may help someone
app/Http/Requests/Auth/LoginRequest.php
public function authenticate()
{
$this‑>ensureIsNotRateLimited();
if (!Auth::attempt($this‑>only('email', 'password'), $this‑>filled('remember')) ||
!auth()‑>user()‑>isAdmin() <‑‑‑‑‑‑‑‑‑‑‑‑ added
) {
Auth::logout(); <‑‑‑‑‑‑‑‑‑‑‑‑ added
RateLimiter::hit($this‑>throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this‑>throttleKey());
}
I added the 'isAdmin' function to the user model, it isn't pre built