简体   繁体   English

在 laravel 8 中使用 jetstream 进行身份验证 - 用户名或 email

[英]authenticating in laravel 8 with jetstream - username or email

I am searching for a solution to change the standard Jetstream Auth to Auth with username or email.我正在寻找将标准 Jetstream Auth 更改为使用用户名或 email 进行身份验证的解决方案。 This means during the registration of the account you enter email or username (username is always required).这意味着在注册帐户期间,您输入 email 或用户名(始终需要用户名)。 In the login form you can enter username or email as your credentials.在登录表单中,您可以输入用户名或 email 作为您的凭据。 I have also changed in the profile settings to update also the username.我还更改了配置文件设置以更新用户名。

My actual problems are我的实际问题是

  1. during registration only with the username I get the error of the empty email field registration error在仅使用用户名注册期间,我收到空 email 字段注册错误的错误

  2. during login the username will be not a correct credential在登录期间,用户名将不是正确的凭据

I have changed everything similar to the post Problem authenticating with username and password in Laravel 8 but it doesn't work.我已经更改了类似于Laravel 8 中使用用户名和密码进行身份验证的帖子问题的所有内容,但它不起作用。

1. change in config/fortify.php 1. 更改config/fortify.php

'username' => 'email' to 'username' => 'identity'

2.added authentication code to app/Providers/FortifyServiceProvider.php inside boot method 2.在app/Providers/FortifyServiceProvider.php里面添加验证码

Fortify::authenticateUsing(function (LoginRequest $request) {
        $user = User::where('email', $request->identity)
            ->orWhere('username', $request->identity)->first();

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

and also added classes并且还添加了类

use Laravel\Fortify\Http\Requests\LoginRequest;
use App\Models\User;

3. added username during register Add input field unter register.blade.php 3.注册时添加用户名在register.blade.php register.blade.php添加输入字段

<div class="mt-4">
            <x-jet-label for="username" value="{{ __('User Name') }}" />
            <x-jet-input id="username" class="block mt-1 w-full" type="text" name="username" :value="old('username')" required autofocus autocomplete="username" />
</div> 

and deleted the required from the email form field.并从 email 表单字段中删除required的内容。

4.add username to the User model 4.将用户名添加到用户model

protected $fillable = [
    'name',
    'email',
    'password',
    'username',
];

change in app/Actions/Fortify/CreateNewUser.php app/Actions/Fortify/CreateNewUser.php中的更改

Validator::make($input, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['string', 'email', 'max:255', 'unique:users'],
        'username' => ['required', 'string', 'max:255', 'unique:users'],
        'password' => $this->passwordRules(),
    ])->validate();

    return User::create([
        'name' => $input['name'],
        'email' => $input['email'],
        'username' => $input['username'],
        'password' => Hash::make($input['password']),
    ]);

5. added username field to database 5. 添加用户名字段到数据库

Schema::table('users', function (Blueprint $table) {
        $table->string('username')->nullable();
    });

In your first case where username is required but email is optional, you will need to add nullable to your validation properties.在需要用户名但 email 是可选的第一种情况下,您需要将nullable添加到验证属性中。 Please check the docs on optional fields .请检查可选字段的文档。

For you second use case, login.blade.php the default input tag is name="email" .对于您的第二个用例login.blade.php默认输入标签是name="email" That means your condition will be $user = User::where('email', $request->email)->orWhere('username', $request->email)->first();这意味着您的条件将是$user = User::where('email', $request->email)->orWhere('username', $request->email)->first();

Check Customizing User Authentication on Laravel Fortify.检查 Laravel Fortify 上的自定义用户身份验证

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

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