Often, we need users to verify the email that they registered in the app. This might be because of security reasons or if we want to be sure that the communications we send to the user reach the mailbox of an existing account.
Laravel offers the easy implementation of email verification through built-in services.
In the User
model, add the interface MustVerifyEmail
. After this, a verification email will arrive when we register a new user in our app.
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Three routes need to be defined to ensure successful email verification.
We can also protect the routes so that only those users who have been verified by email have access.
This is done using the route middleware
, which is implemented as follows:
Route::get('/profile', function () {
// Only verified users may access this route...
})->middleware('verified');