kumari shwetha
kumari shwetha

Reputation: 81

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');
        };
    }

Upvotes: 4

Views: 5010

Answers (5)

Temidayo
Temidayo

Reputation: 92

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;

Upvotes: 3

ef few
ef few

Reputation: 37

Maybe add this in the Routes/web.php. And change the /dashboard to whatever you like :).

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
 
    return redirect('/dashboard');
})->middleware(['auth', 'signed'])->name('verification.verify');

Upvotes: 0

Bird87 ZA
Bird87 ZA

Reputation: 2160

If you are using Fortify, I think the more Laravel way to solve this, is to create a Response handler.

Laravel will, when an email is verified, generate a VerifyEmailResponse. So to modify that logic, you would create a class and make that the singleton.

<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;

class VerifyEmailResponse implements VerifyEmailResponseContract
{
    public function toResponse($request)
    {
        // Your custom logic returning redirect(), \Illuminate\Routing\Redirector or \Illuminate\Http\RedirectResponse
    }
}

And in your FortifyServiceProvider class:

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;



public function boot()
{
    // logic
    $this->app->singleton(VerifyEmailResponseContract::class, VerifyEmailResponse::class);
    // logic
}

Upvotes: 2

Snapey
Snapey

Reputation: 4110

Add a method called redirectTo(). It will be called if it exists.

public function redirectTo()
{
   // put your routing logic here
}

The function should return a string of the url to go to.

Upvotes: 3

user18716605
user18716605

Reputation:

Couple approaches from what I can see...

  1. In your controller actions where you are routing them to Auth\VerificationController@verify do the check to make sure a flag isn't already set, set the flag if it isn't set, put your redirect after that logic. So it would be something like this for a route...

    return redirect()->route('route.name', [$veriYouWantToSendAlong]);

Or to a static page with errors.

return redirect()->route('page/name')->withErrors(['Some error here']);
  1. In your Laravel auth controller should be a protected $redirectTo = '/home'; action you can change that to dashboard or wherever you'd like to send them after log in.

Upvotes: 0

Related Questions