简体   繁体   English

如何修改 Laravel Jetstream 登录

[英]How to modify Laravel Jetstream login

I am using Larave Jetstream livewire and I want to modify the login.我正在使用 Larave Jetstream livewire,我想修改登录。

login from having a hidden input field "is_admin" with an initial value of 1从具有初始值为 1 的隐藏输入字段“is_admin”登录

when a user submits login form backend check with is_admin = 1 of the database table field当用户使用数据库表字段的is_admin = 1 提交登录表单后端检查时


table structure: name, email, password, is_admin表结构:姓名、邮箱、密码、is_admin

is_admin = 0 or 1 is_admin = 0 或 1

I want to check the is_admin flag, If provided credentials match email , password , and is_admin=1 then only the user can log.我想检查 is_admin 标志,如果提供的凭据与emailpasswordis_admin=1匹配,则只有用户可以登录。


Please help me with this.请帮我解决一下这个。

Thank you.谢谢你。

I think you intend to customise authentication.我认为您打算自定义身份验证。 Since you are using Jetstream most probably you are using Fortify out of the box (by default).由于您使用的是 Jetstream,因此很可能您使用的是开箱即​​用的 Fortify(默认情况下)。

There are 2 ways.有2种方式。 These are to help you sent extra data from your authentication form and not just the hidden field.这些是为了帮助您从身份验证表单发送额外的数据,而不仅仅是隐藏字段。 However, if the is_admin field is default, then I don't think you should add it as hidden field.但是,如果 is_admin 字段是默认值,那么我认为您不应该将其添加为隐藏字段。 You could be compromised.你可能会受到损害。

Example 1. Edit the User.php model and add a boot method Fortify::authenticateUsing示例 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 https://laravel.com/docs/8.x/fortify#customizing-user-authentication

Example 2. Also you may edit the app/Providers/FortifyServiceProvider.php示例 2. 您也可以编辑app/Providers/FortifyServiceProvider.php

And in the boot method add并在引导方法中添加

Fortify::authenticateUsing(function (Request $request){
    $is_admin = $request->is_admin ?? 1;
    
    // Other codes here
});

Also unless I did not understand you correctly but just want to be sure the user is admin before allowing login, then you could tweak the auth code in Example 1 to do that.此外,除非我没有正确理解你,但只是想在允许登录之前确保用户是管理员,然后你可以调整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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM