0
votes

Recently I am implementing email verification using Laravel 5.7.

I found the email verification is not working if you follow the following scenario.

Scenario 1

User registered an account on browser A and activate it on browser B.

Then it will redirect user to the login page.

Obviously email activation failed.

Scenario 2 (For my use case)

User registered an account on browser A.

The activation email was sent to the mobile inbox.

User activate the email from the mobile and the activation is not working.


I am guessing the activation failed if using another browser agent to activate was because the activation key session key was not found.

Anyone is facing the same issue and managed to come out with a solution?

2

2 Answers

4
votes

Because the way Laravel email verification works, it required user to be logged to be able to process the verify link. You can fix this by changing the following in VerificationController

  1. In the __construct() function make auth middleware exception for verify function
    $this->middleware('auth')->except('verify');
  1. Create a verify function to override laravel verify function
    public function verify(Request $request)
    {
        $userId = $request->route('id');
        $user = User::findOrFail($userId);

        if ($user->hasVerifiedEmail()) {
            return redirect($this->redirectPath());
        }

        if ($user->markEmailAsVerified()) {
            event(new Verified($user));
        }

        return redirect($this->redirectPath())->with('verified', true);
    }
0
votes

To the answer @Gopal Gautam. It worked for me exactly like this:

public function verify(Req $request)
{         
    $userId = $request->route('id');
    $user = \App\User::findOrFail($userId);

    if ($user->hasVerifiedEmail()) {
        \Illuminate\Support\Facades\Auth::login($user);
        return redirect($this->redirectPath())->with('verified', true);
    }

    if ($user->markEmailAsVerified()) {
        event(new Verified($user));
    }

    return redirect($this->redirectPath())->with('verified', true);
}