我正在使用Larave Jetstream livewire,我想修改登录。
从具有初始值为1的隐藏输入字段"is_admin“登录
用户提交登录表单后端检查数据库表字段is_admin =1
表结构:名称、邮箱、密码、is_admin
is_admin =0或1
我想检查is_admin标志,如果提供的凭据与email、password和is_admin=1匹配,则只有用户可以登录。
发布于 2021-10-23 10:32:37
我认为您打算自定义身份验证。由于您使用的是Jetstream,因此您很可能正在使用Fortify (默认情况下)。
有两种方法。这些是为了帮助您从身份验证表单发送额外的数据,而不仅仅是隐藏字段。但是,如果is_admin字段是默认字段,那么我认为您不应该将其添加为隐藏字段。你可能会受到威胁。
示例1.编辑User.php模型并添加引导方法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
});此外,除非我没有正确理解您,但只是想确保用户是管理员才允许登录,然后您可以调整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;
}
});https://stackoverflow.com/questions/69687163
复制相似问题