How do you customize the default login error message in laravel 8

Viewed 1556

I was browsing through the web and looking for solution on how can I modify this error message on Jetstream login:

enter image description here

Inside app/Actions/Fortify there is a file CreateNewUser.php where I can put some validation custom message on each field like this:

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

However, there is none for Login.php. I search the web and saw something about vendors/laravel/fortify/src/Actions/AttemptToAuthenticate.php but its consist with lots of code where I don't know where to put the customization:

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

Is there an easy way to customize the "The email field is required" to a different custom message in Laravel 8?

Been stuck for a couple of hours.

2 Answers

You can customize FortifyServiceProvider inside App > Providers by using the Fortify::authenticateUsing method and it should be called from the boot method.

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

Reference

step 1 go to vendor\laravel\fortify\src\Http\Requests\LoginRequest.php step 2 in the loginRequest.php you will find two method

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

step 3 add the following method below

public function messages() {
  return = [
  'email.required' => 'email required',
  'password.required' => 'password required'
  ];
}

Related