如何修改Laravel Jetstream登录



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

从隐藏输入字段"is_admin"初始值为1

当用户提交登录表单后端检查数据库表字段

is_admin= 1时

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

is_admin = 0 or 1

我想检查is_admin标志,如果提供的凭据匹配email,passwordis_admin=1,那么只有用户可以登录。


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

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

示例1。编辑User.php模型并添加一个引导方法Fortify::authenticateUsing

use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;
use LaravelFortifyFortify;
/**
* 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

在boot方法中添加

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

// Other codes here
});

另外,除非我没有正确理解你,但只是想确保用户是允许登录之前的管理员,那么你可以调整Example 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;
}
});

最新更新