0
votes

I am working on a requirement where I send emails after record update/insert in which I send page URL as "click here" text, when user clicks on that link he will be redirected to that page regardless of user logs in or not.

Now if the user already logged-in then that page normally opens but if user is not logged-in then I need to redirect back to previous page after login.

My Problem here is I am unable to redirect back to the previous page which user tried to access before login through 'click here' link from email

I have searched for the solutions I found few but they were not working. One of the solution I tried is Scott's answer but din't work why because our application authentication is totally customized. Our RedirectIfAuthenticated file code is below

NOTE: Our application has been built on Laravel

RedirectIfAuthenticated.php

class RedirectIfAuthenticated
{
  public function handle($request, Closure $next, $guard = null)
  {
    $path = $request->path();
    if (Auth::guard($guard)->check()) {
        if($path == 'app1')
        {
            return redirect('/app1/dashboard');
        }
        elseif($path == 'app2')
        {
            return redirect('/app2/dashboard');
        }
    }

    return $next($request);
  }
}

Any help would be appreciated. Thanks

4
are you use ajax?Manisha
return redirect()->route('route_name'); ?Lim Kean Phang
what is 'route_name' in my case?Prasad Patel

4 Answers

1
votes

what you need to do is get the previous URL and check with that and redirect to relevant dashboard you can use URL::previous() in URL Facades. you can do something like this,

use Illuminate\Support\Facades\URL;

class RedirectIfAuthenticated
{
   public function handle($request, Closure $next, $guard = null)
   {
      $path1 = url('app1/login')
      $path2 = url('app2/login')

      if (Auth::guard($guard)->check()) {
        if($path1 == URL::previous())
        {
           return redirect('/app1/dashboard');
        }
        elseif($path2 == URL::previous())
        {
            return redirect('/app2/dashboard');
        }
    }

    return $next($request);
  } 
}

Hope this helps. if you just want check app1 or app2 I suggest that you explode() the previous() and check

0
votes

I think you have to redirect on back page

class RedirectIfAuthenticated
{
  public function handle($request, Closure $next, $guard = null)
  {
    $path = $request->path();
    if (Auth::guard($guard)->check()) {
        return redirect()->back();
    }
  }
}
0
votes

Try this

override this method in Auth\LoginController

protected function authenticated(Request $request, $user)
{
     if ($request->expectsJson() || $request->ajax()) {
        // return login success in json
     }
     return redirect()->intended($this->redirectPath());
}

Eg. if you are trying to access /user/send/email and the user is not logged in then it will redirect to login page once the user is logged in then it will automatically redirect to /user/send/email page

RedirectIfAuthenticated

class RedirectIfAuthenticated
{
     public function handle($request, Closure $next, $guard = null)
     {
         $path = $request->path();
         if ( auth()->guard($guard)->check() ) {
            return redirect()->route($path.'.dashboard');
            // or
            // return redirect()->route($guard.'.dashboard'); //Suggest: define route based on guard
        }
        return $next($request);
     }

}

Register route as app1.dashboard and app2.dashboard

0
votes

Store the path in session variable, if the user is not logged in. After successful login, check if the session value is set, redirect the user to the value set and before redirecting the user do not forget to unset the session

public function handle($request, Closure $next, $guard = null)
{
        $path = $request->path(); 
        $url = "";
        if($path == 'app1')
        {
           $url="/app1/dashboard";             
         }
        elseif($path == 'app2')
        {
           $url="/app2/dashboard";             
        }

        if (Auth::guard($guard)->check()) {
              return redirect($url);
        }
        else{
          // create session variable and store the path 
          // save `$url` to session variable
        }

      return $next($request);
 }