1

I want to redirect user to previous url after email verification.

Normally in Laravel 5.7 after you click on route which requires authentication you will be redirected back to that url after you log in.

I need same behaviour after e-mail verification.

6
  • You will have to store that url somewhere so you can restore it afterwards. Commented Dec 14, 2018 at 16:58
  • Not massively familiar with Laravel, but couldn't you just override it an auth controller? Commented Dec 14, 2018 at 17:01
  • What do you mean with previous url? The previous url will be likely the registration page. Commented Dec 14, 2018 at 17:02
  • I just found the way to change it. Added as an answer. Thanks for suggestions. Commented Dec 14, 2018 at 17:06
  • If there is an easier way I am still open for suggestions. Commented Dec 14, 2018 at 17:07

1 Answer 1

6

First we need to modify RedirectIfAuthenticated and put intended url to session.

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if(session()->get("url.intended")){
            session()->put("redirect_after_email_verification", session()->get("url.intended"));
        }

        if (Auth::guard($guard)->check()) {
            return redirect('/admin');
        }

        return $next($request);
    }
}

Then add following code to VerificationController to check previously added url to session. If this url exist in session we will redirect user to that url after email verification.

public function show(Request $request)
{
    return $request->user()->hasVerifiedEmail()
        ? redirect($this->redirectPath())
        : view('auth.verify');
}

protected function redirectTo(){
    if(session()->get("redirect_after_email_verification")){
        return session()->get("redirect_after_email_verification");
    }

    return $this->redirectTo;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.