Search code examples
laravelmodel-view-controllerlaravel-8email-verificationlaravel-authentication

how to change redirect after auth Email verification in laravel 8?


I have 2 condition after successful registration with email verification.

  1. If the new user is select plan from home page, redirects to registration page submits the form. then will get Email verfication link, and after email verified I want to redirect directly to checkout. Plan id will be saving session , so I can get all the details of plan.
  2. If the new user is not select plan from home page, then he can sign up and redirects to dashboard

But in laravel after email verfication always redirects to home page. But I dont want to redirect to home page again.

How can be this done? Wher can do the coding part?

Verification Controller


 use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
    
    protected function verified(Request $request)
    {
        $request->session()->flash('alert','Your Email is verfied');
    }


Routes

  public function emailVerification()
    {
        return function () {
            $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
            $this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
            $this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
        };
    }

Solution

  • The best option that worked for me in Laravel 9* was to define a new public constant in the RouteServiceProvider right under or above the HOME e.g Location: app\Providers\RouteServiceProvider

     public const SUB = '/account/subscription';
    

    Then go to VerificationController Location: app\Http\Controllers\Auth\VerificationController

    and change

      protected $redirectTo = RouteServiceProvider::HOME;
    

    to

      protected $redirectTo = RouteServiceProvider::SUB;