Change email verification error message Laravel 5.7

Viewed 1948

I am using Laravel 5.7 email verification. When I try to log in it says - Your email address is not verified
http://joxi.ru/n2YqYJaIo1XGO2 I need to change this message to another language but could not find where to change. In resources/lang - there are validations and other messages but could not find this one. Thanks.

3 Answers

Better solution, use this instead.


You'll have to implement a custom middleware which you can create with the artisan command:

php artisan make:middleware EnsureEmailIsVerified

EnsureEmailIsVerified.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class EnsureEmailIsVerified
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
     public function handle($request, Closure $next)
     {
         if (! $request->user() ||
             ($request->user() instanceof MustVerifyEmail &&
             ! $request->user()->hasVerifiedEmail())) {
             return $request->expectsJson()
                     ? abort(403, 'YOUR CUSTOM ERROR HERE')
                     : Redirect::route('verification.notice');
         }

         return $next($request);
     }
}

You will have to map the verified key in your kernel file to the new middleware.

app\Http\Kernel.php (down at the bottom):

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class, // the changed line
    ];

Original answer


I used Notepad++'s find in file function and scanned all the files in my Laravel project for: "Your email is not verified"

It came up with one match in:

\vendor\laravel\framework\src\Illuminate\Auth\Middleware\EnsureEmailIsVerified.php

Which is this file:

<?php

namespace Illuminate\Auth\Middleware;

use Closure;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class EnsureEmailIsVerified
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle($request, Closure $next)
    {
        if (! $request->user() ||
            ($request->user() instanceof MustVerifyEmail &&
            ! $request->user()->hasVerifiedEmail())) {
            return $request->expectsJson()
                    ? abort(403, 'Your email address is not verified.')
                    : Redirect::route('verification.notice');
        }

        return $next($request);
    }
}

I'm guessing that if you change this line: ? abort(403, 'Your email address is not verified.')

To whatever you want the error to be, for example: ? abort(403, 'Please, verify your email.')

That it will display that. (Please confirm this if you try it).


There's one minor issue with this solution. Since your .gitignore file tells git to ignore the vendor folder, it won't be pushed to an external repo when pushing.

You will need to change the .gitignore file.

Laravel updates will also revert this change, so you will have to rewrite it, there's a better solution at the top of this answer now.

Follow this path in your laravel application "\vendor\laravel\framework\src\Illuminate\Auth\Middleware\EnsureEmailIsVerified.php" You will find the error message "Your email address is not verified" here on in `

public function handle($request, Closure $next)
    {
        if (! $request->user() ||
            ($request->user() instanceof MustVerifyEmail &&
            ! $request->user()->hasVerifiedEmail())) {
            return $request->expectsJson()
                    ? abort(403, 'Your email address is not verified.')
                    : Redirect::route('verification.notice');
        }

        return $next($request);
    }

You can overwrite the file directly as others have said, but when Laravel updates, this file will be overridden by Composer.

You should make a new Middleware class with the contents of the EnsureEmailIsVerified class, and change the declaration of the 'verified' middleware in the app/Http/Kernel.php file:

protected $routeMiddleware = [
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    // change to:
    'verified' => \Your\Custom\Middleware::class,
];

You could even make the abort message use a lang file, so you can change it in future.

Related