使用用户名或电子邮件登录 Laravel fortify 验证失败

时间:2021-06-19 00:04:02

标签: php laravel laravel-fortify

我是 Laravel 的新手,我正在创建一个登录系统,用户可以在其中使用用户名或电子邮件登录。当我提交表单时,它返回错误 The username field is required.

登录请求.php

public function rules()
    {
        return [
            Fortify::username() => 'required|string',
            'password' => 'required|string',
        ];
    }

FortifyServiceprovider.php

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

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

Fortify.php

'features' => [
        Features::registration(),
        Features::resetPasswords(),
        Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication([
            'confirmPassword' => true,
        ]),
    ],

登录.blade.php

<form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>

请任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

为输入字段用户名和密码添加名称属性

登录.blade.php

    <form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" name="username" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>