Chron's avatar
Level 6

Verify Email Address redirect when not logged in

For example, I have this verification link.

http://localhost:8000/customer/email/verify/5?expires=1562735461&signature=e1fa4041febb93926d09fab13c743b27b5cfb0c4a36c84d1147d11d2bd480c1d

If the user isn't logged in yet, it should redirect to the /customer/login page. Is there a way to do that?

0 likes
4 replies
bobbybouwmann's avatar

Well you can override the default route from Laravel in your own routes file and add the auth middleware.

// web.php

Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify')->middleware('auth');

Another solution is adding the auth middleware to the controller itself

// VerificationController.php

public function __construct()
{
    $this->middleware('log')->only('verify');
}

Documentation: https://laravel.com/docs/5.8/controllers

Chron's avatar
Level 6

Well you can override the default route from Laravel in your own routes file and add the auth middleware.

I already had that inside of the controllers.

markus.heb's avatar

In your Authenticat Middleware add the following

    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return url('/customer/login');
        }
    }

I added a check for Ajax calls, if you don't want that you can also only return the url.

mstrauss's avatar

Quick Note: Half way through writing this answer, it occurred to me that you should probably use @markus.heb advice, as it is much simpler than what is included here. But, I wanted to finish the answer anyway just in case someone wants to dig a bit deeper into the framework and how some of this stuff works, good luck!

So if you have the below in your VerificationController:

public function __construct()
{
    $this->middleware('auth')->only('verify');
}

Then the default auth redirect in Laravel should look like this (depending on your version of course):

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

So, assuming you have Auth::routes(); in your routes/web file, you can override the default get login, below:

$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');

By changing the URL, like below and placing it after the Auth::routes();call. You will end up having two routes with the name login but since your custom route comes after the original, it will be used.

Route::get('/customer/login', 'Auth\LoginController@showLoginForm')->name('login');

If you are interested as to why this works:

Auth:routes()calls:

// framework Support/Facades/Auth.php

    /**
     * Register the typical authentication routes for an application.
     *
     * @param  array  $options
     * @return void
     */
    public static function routes(array $options = [])
    {
        static::$app->make('router')->auth($options);
    }
// framework Routing/Router.php

    /**
     * Register the typical authentication routes for an application.
     *
     * @param  array  $options
     * @return void
     */
    public function auth(array $options = [])
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');

        // Registration Routes...
        if ($options['register'] ?? true) {
            $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
            $this->post('register', 'Auth\RegisterController@register');
        }

        // Password Reset Routes...
        if ($options['reset'] ?? true) {
            $this->resetPassword();
        }

        // Email Verification Routes...
        if ($options['verify'] ?? false) {
            $this->emailVerification();
        }
    }

Please or to participate in this conversation.