gpt4 book ai didi

php - 如何修改 Laravel Jetstream 登录

转载 作者:行者123 更新时间:2023-12-05 03:36:12 26 4
gpt4 key购买 nike

我正在使用 Larave Jetstream livewire,我想修改登录。

使用初始值为 1 的隐藏输入字段“is_admin”登录

当用户提交登录表单时,后端检查数据库表字段的 is_admin = 1


表结构:姓名、电子邮件、密码、is_admin

is_admin = 0 或 1

我想检查 is_admin 标志,如果提供的凭据与 emailpasswordis_admin=1 匹配,则只有用户可以登录.


最佳答案

我认为您打算自定义身份验证。由于您使用的是 Jetstream,很可能您使用的是开箱即​​用的 Fortify(默认情况下)。

有两种方法。这些是为了帮助您从身份验证表单发送额外数据,而不仅仅是隐藏字段。但是,如果 is_admin 字段是默认字段,那么我认为您不应该将其添加为隐藏字段。您可能会受到损害。

例1.编辑User.php模型,添加boot方法Fortify::authenticateUsing

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Fortify::authenticateUsing(function (Request $request) {
$is_admin = $request->is_admin ?? 1;

// Your code here ... Example for login in below
$user = User::where('email', $request->email)->first();

if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
});

// ...
}

https://laravel.com/docs/8.x/fortify#customizing-user-authentication

示例 2。您也可以编辑 app/Providers/FortifyServiceProvider.php

并在启动方法中添加

Fortify::authenticateUsing(function (Request $request){
$is_admin = $request->is_admin ?? 1;

// Other codes here
});

此外,除非我没有正确理解您的意思,只是想在允许登录之前确保用户是管理员,否则您可以调整示例 1 中的授权代码来做到这一点。

Fortify::authenticateUsing(function (Request $request) {

$user = User::where(['email' => $request->email, 'is_admin' => 1])->first();

if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});

关于php - 如何修改 Laravel Jetstream 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69687163/

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