Search code examples
laravelemail-verification

Apply Laravel 5.7 MustVerifyEmail on Multiple Authentication System


I'm trying to apply Laravel-5.7 MustVerifyEmail on multiple authentication system. So far what I've done is as follows:

  1. created verification routes for the 'auditor' guard.
  2. overwrite the show method in Verification controller with a new view.
  3. Implemented a new notification in Auditor Model.
  4. Created, register and applied a new middleware called 'auditor.verified'

After this procedure, I find that it's sending a notification to email and shows the verify page but when I click on the 'Verify Email Address' button in the mail it update the database with the timestamp but it don't take me to the redirect page. Instead, I get "The page isn't working" message in the browser.

There should be something I missed.

Here is the project file on GitHub

Thanks in advance for your help.


Solution

  • Finally, after four days of research I was able to solve the issue.

    I altered the "EnsureEmailIsVerified" middleware as follows:

    <?php
    
    namespace Illuminate\Auth\Middleware;
    
    use Closure;
    use Illuminate\Support\Facades\Redirect;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Support\Facades\Auth;
    
    class EnsureEmailIsVerified
    {
    
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle($request, Closure $next, $guard = null)
    {
    
        $guards = array_keys(config('auth.guards'));
    
        foreach($guards as $guard) {
    
            if ($guard == 'admin') {
    
                if (Auth::guard($guard)->check()) {
    
                    if (! Auth::guard($guard)->user() ||
                        (Auth::guard($guard)->user() instanceof MustVerifyEmail &&
                        ! Auth::guard($guard)->user()->hasVerifiedEmail())) {
                        return $request->expectsJson()
                                ? abort(403, 'Your email address is not verified.')
                                : Redirect::route('admin.verification.notice');
                    }  
    
                }
    
            }
    
            elseif ($guard == 'auditor') {
    
                if (Auth::guard($guard)->check()) {
    
                    if (! Auth::guard($guard)->user() ||
                        (Auth::guard($guard)->user() instanceof MustVerifyEmail &&
                        ! Auth::guard($guard)->user()->hasVerifiedEmail())) {
                        return $request->expectsJson()
                                ? abort(403, 'Your email address is not verified.')
                                : Redirect::route('auditor.verification.notice');
                    }  
    
                }
    
            }
    
            elseif ($guard == 'web') {
    
                if (Auth::guard($guard)->check()) {
    
                    if (! Auth::guard($guard)->user() ||
                        (Auth::guard($guard)->user() instanceof MustVerifyEmail &&
                        ! Auth::guard($guard)->user()->hasVerifiedEmail())) {
                        return $request->expectsJson()
                                ? abort(403, 'Your email address is not verified.')
                                : Redirect::route('verification.notice');
                        }  
    
                    }
                }
    
            }
    
            return $next($request);
        }
    }
    

    And that's solved my problem.