laravel 微風 Multi Auth - 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth - Admin Guard with two diffirent registration)


問題描述

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

(by HaronHaronpszaba)

參考文件

  1. laravel breeze Multi Auth ‑ Admin Guard with two diffirent registration (CC BY‑SA 2.5/3.0/4.0)

#laravel-8 #authentication #PHP #laravel-breeze #laravel-guard






相關問題

在 Laravel 的表中計算 user_id (Count user_id in a table in Laravel)

如何通過模型顯示表中的數據,自定義行除外 (How to show data from table via Model except a custom row)

如何在 Laravel 的模型中為所有貨幣字段(十進制屬性)加上逗號 (How to put comma to all money fields (decimal attributes) in a model in Laravel)

Laravel:8.x 目標類 [ArticlesController] 不存在 (Laravel:8.x Target class [ArticlesController] does not exist)

如何根據 laravel 中的一列獲取最新記錄? (How to get latest record based on one column in laravel?)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)

更改徽標默認通知電子郵件 laravel 8 (change logo default notification email laravel 8)

Laravel 8 - 如果用戶有權調用路由,請簽入控制器 (Laravel 8 - Check in Controller if user has permission to call route)

laravel 微風 Multi Auth - 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth - Admin Guard with two diffirent registration)

找不到組件的類或視圖 (Unable to locate a class or view for component)

Laravel 8 數據庫不會使用 HTML 表單更新 (Laravel 8 database won't update using HTML form)

Laravel問題從電子郵件驗證傳遞錯誤消息 (Laravel problem to pass error message from email verification)







留言討論