Mit*_*ari 5 php laravel laravel-livewire laravel-jetstream
我正在使用 Larave Jetstream livewire,我想修改登录名。
从具有初始值为 1 的隐藏输入字段“is_admin”登录
当用户提交登录表单时,后端检查is_admin数据库表字段的 = 1
表结构:姓名、电子邮件、密码、is_admin
is_admin = 0 或 1
我想检查 is_admin 标志,如果提供的凭据匹配email, password,则is_admin=1只有用户可以登录。
我认为您打算自定义身份验证。由于您使用的是 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;
}
});
// ...
}
Run Code Online (Sandbox Code Playgroud)
https://laravel.com/docs/8.x/fortify#customizing-user-authentication
示例 2. 您也可以编辑app/Providers/FortifyServiceProvider.php
并在启动方法中添加
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;
}
});
// ...
}
Run Code Online (Sandbox Code Playgroud)
另外,除非我没有正确理解您的意思,但只是想在允许登录之前确保用户是管理员,那么您可以调整身份验证代码来Example 1做到这一点。
Fortify::authenticateUsing(function (Request $request){
$is_admin = $request->is_admin ?? 1;
// Other codes here
});
Run Code Online (Sandbox Code Playgroud)