Laravel 8 使用 Guards Rest 密码进​​行多重身份验证问题

Hil*_*jem 1 php authentication laravel laravel-8

我正在尝试使用守卫制作一个具有多重身份验证的 Laravel 应用程序。

Github: 存储库

我创建了一个新的 Laravel 8 应用程序并添加了管理员防护,现在我可以在管理员和用户模式下登录和注销。

这张图片显示了我所做的详细信息,您可以跳过并阅读下面的管理员重置密码部分 详情图片

配置/auth.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 30,
            'throttle' => 30,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

Run Code Online (Sandbox Code Playgroud)

管理员重置密码部分

我将流动函数添加到PasswordResetLinkController and NewPasswordController for admin

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard('admin');
}

/**
 * Returns the password broker for admins
 * 
 * @return broker
 */
protected function broker()
{
    return Password::broker('admins');
}
Run Code Online (Sandbox Code Playgroud)

然后我去/admin/forget-password路由并提交了管理员电子邮件,但出现以下错误:We can't find a user with that email address.

检查后,我发现在store的功能中PasswordResetLinkController它正在尝试在users table/model. 这是函数存储:

/**
 * Handle an incoming password reset link request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\RedirectResponse
 *
 * @throws \Illuminate\Validation\ValidationException
 */
public function store(Request $request)
{
    $request->validate([
        'email' => 'required|email',
    ]);

    // We will send the password reset link to this user. Once we have attempted
    // to send the link, we will examine the response then see the message we
    // need to show to the user. Finally, we'll send out a proper response.
    $status = Password::sendResetLink(
        $request->only('email')
    );

    return $status == Password::RESET_LINK_SENT
                ? back()->with('status', __($status))
                : back()->withInput($request->only('email'))
                        ->withErrors(['email' => __($status)]);
}
Run Code Online (Sandbox Code Playgroud)

如何强制PasswordResetLinkController 和NewPasswordController 类使用我添加到其中的guard() 和broker() 函数?

抱歉,我试图在多个平台上搜索这个问题,他们只使用角色方法来完成长解释,如果我没有彻底解释,有些人可能不明白我想要做什么

小智 5

store()由于您使用的是 Laravel Breeze,因此请在类的方法中替换这行代码PasswordResetLinkController.php

$status = Password::sendResetLink($request->only('email'));
Run Code Online (Sandbox Code Playgroud)

与以下行。

Password::broker('admins')->sendResetLink($request->only('email'))
Run Code Online (Sandbox Code Playgroud)