php - laravel 8 中如何自定义默认登录错误信息
问题描述
我正在浏览网页并寻找有关如何在 Jetstream 登录时修改此错误消息的解决方案:
里面app/Actions/Fortify
有一个文件CreateNewUser.php
,我可以在每个字段上放置一些验证自定义消息,如下所示:
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:17'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
],[
'name.required'=>'Name is required',
'email.required'=>'Email is required',
'phone.required'=>'Phone is required',
])->validate();
但是,对于Login.php
. 我在网上搜索并看到了一些关于vendors/laravel/fortify/src/Actions/AttemptToAuthenticate.php
但它包含很多我不知道在哪里放置自定义的代码:
public function handle($request, $next)
{
if (Fortify::$authenticateUsingCallback) {
return $this->handleUsingCustomCallback($request, $next);
}
if ($this->guard->attempt(
$request->only(Fortify::username(), 'password'),
$request->filled('remember'))
) {
return $next($request);
}
$this->throwFailedAuthenticationException($request);
}
是否有一种简单的方法可以将“需要电子邮件字段”自定义为 Laravel 8 中的不同自定义消息?
被困了几个小时。
解决方案
您可以使用该方法FortifyServiceProvider
在内部进行自定义,并且应该从该方法中调用它。App > Providers
Fortify::authenticateUsing
boot
public function boot()
{
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
} else {
$request->session()->flash('message', 'Invalid!');
return false;
}
});
}
推荐阅读
- javascript - 如何将 Javascript 中的对象数组解构为 ES6 中的两个预定义变量?
- sql-server - 如何使用来自 Google Big Query 的文件在 SQL Server 中使用批量导入?
- java - 当引用的方法抛出异常时,如何以方法为参数调用另一个方法?
- npm - npm inssue babelify@10.0.0 需要 @babel/core@^7.0.0 的 peer
- java - JAVA 如何在java中追加一个列表和TableView?
- c# - 在项目中查找程序集依赖项
- javascript - 如何在单选按钮中从数据库加载值?
- reactjs - ESLint Prefer default export import/prefer-default-export
- jquery - 成功上传表单(使用ajax)后如何自动更新下拉列表(使用jQuery)
- c# - C#将选择命令从一个类发送到另一个类并返回结果?