首页 > 解决方案 > 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 中的不同自定义消息?

被困了几个小时。

标签: phplaravel

解决方案


您可以使用该方法FortifyServiceProvider在内部进行自定义,并且应该从该方法中调用它。App > ProvidersFortify::authenticateUsingboot

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;
        }
    });
}

参考


推荐阅读