MrSandyWilly
MrSandyWilly

Reputation: 548

Laravel Fortify email verification redirects to /home

I'm using Laravel Fortify for the auth of my project. After a user has registered, it should be redirected to a page telling it that a mail have been sent - however, it redirects to /.

As per the docs I have done the following:

  1. My User model implements MustVerifyEmail.
  2. I've created the view I want to show (views/auth/verify-email.blade.php).
  3. I've got the following lines in the boot method of FortifyServiceProvider:
Fortify::verifyEmailView(function () {
    return view('auth.verify-email');
});

However, after the user registers it is redirected to / and not the view I specified!

Help appreciated.

Upvotes: 1

Views: 2046

Answers (1)

matiaslauriti
matiaslauriti

Reputation: 8082

As the documentation says:

If the registration attempt is successful, Fortify will redirect the user to the URI configured via the home configuration option within your application's fortify configuration file. If the login request was an XHR request, a 200 HTTP response will be returned.

So, if you have not changed your fortify.home config, it will be defaulted to /home (as that is the default in the config file).

Here is the source code so you can see what it will do ONCE the user is registered, and this is the response (last line of code).

Fortify::verifyEmailView corresponds to showing a verification to the user that it must verify it email, but as the documentation says:

After registration, you may wish for users to verify their email address before they continue accessing your application.

So, this will NOT show the message IF the user is not logged in or was just registered WITHOUT being logged in.

How does this work ? This is done thanks to Laravel itself, it uses a Middleware to do this, and Fortify just uses these standards. This is the source code of the EnsureEmailIsVerified middleware.

If I am not confused, this is not on by default, so you have to tell which routes you would like this to be working on or turn it on as a global middleware. This is the default config for all middlewares in Laravel 8.

Upvotes: 1

Related Questions