php - 使用用户名或电子邮件登录 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;
});
强化.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>
请任何帮助将不胜感激。谢谢
解决方案
将名称属性添加到输入字段用户名和密码
登录.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>
推荐阅读
- python - 锁定命令 discord.py
- python - 没有时间漂移的 Python 异步生成器函数
- python - yolov5 AssertionError: 多个文件匹配
- histogram - hist.default 中的错误:“x”必须是数字
- amazon-web-services - 使用 AWS CLI 时访问 AWS 不足
- go - 在 logrus 日志记录中使用 struct 代替 map
- intellij-idea - IntelliJ IDEA 中的新 Find Usage 弹出窗口将代码片段放在最后(不可见)
- python - Matplotlib 图不会在每个循环中更新
- html - 使用 css 和 bootstrap 重新调整图像缩略图的大小
- python - 按月、周、日对熊猫数据框进行分组的行为不同