Email verification is the process of checking and authenticating emails that users have been provided in the registration form.
To enable this feature we need to implement MustVerifyEmail
interface in our User model.
use Illuminate\Contracts\Auth\MustVerifyEmail;
…
class User extends Authenticatable implements MustVerifyEmail
{
…
}
After that, an email will be sent out when a user registers with a link to verify their email.
However, we still need to add a middleware to our routes where we want to restrict access to unverified users.
We will create a new route called ‘only-verified’ and we will add ‘auth’ and ‘verified’ middleware. The auth middleware prevents access to guests and the verified middleware checks whether the user has verified their email.
Here is an example:
Route::get('/only-verified', function () {
return view('only-verified');
})->middleware(['auth', 'verified']);
Top comments (0)