Amathon's avatar

Redirect::back after login

Hi there,

I have a method which bookmarks an article for the currently logged in user, contained within a controller which uses the "auth" middleware.

This method uses the redirect()->back() function to get the user back to where he was before, since it can be called from various places on the site.

If the user is currently logged in, the method works as intended: it marks the article and redirects the user back to the previous page.

If however the user is currently not logged in, the redirect()->back() takes him back to the login page instead of the previous page.

Does anyone now a solution which works in both scenarios, both logged in and not logged in?

Thanks!

0 likes
8 replies
Juggernation's avatar

Hello, Amathon

I think the problem you are having comes from the 'auth' middleware's behaviour. If you take a look inside it, you will see that non-authenticated users are simply redirected to the '/login' page. In your case, it means that if you are not logged in, redirect()->back() never fires.

A simple way to fix this is to use a custom middleware, which you create. The only real thing you would have to change in it is the handle() method like this:

    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->back();
            }
        }

        return $next($request);
    }

The result would be that for both authenticated users and guests, you will be doing a redirect back. The difference is that for guests, the article will not be marked, as the middleware will not allow that.

Hope that helps! Good luck!

bashy's avatar

Also, you can just use

return back();
Amathon's avatar

Hi @Juggernation, thanks for your input!

Here's the thing: I want to redirect the user to the login page. After logging in, the "bookmark" function should trigger, do the database stuff and redirect him to the page he was before logging in (where he clicked on "bookmark"). But the back() function sends him back to the login page instead.

Here's how it should be:

Logged in: user clicks 'bookmark' link--> bookmark() method fires, does database stuff and sends the user back via back(). Logged out: user clicks 'bookmark' link --> redirect to login -> user fills login details -> bookmark() method fires and sends the user back.

Any suggestions?

bashy's avatar

@Amathon back() just does a redirect based on referrer... Use return redirect(route('some.name));

Amathon's avatar

Hi @bashy,

I want to call the action from different locations on the site (one of which is a search results page with tons of get variables), not just from one page, that's why wanted to use back(). Just thought there may be a 'laravel' way to do this. :-)

Should I maybe just send the current url as a variable with the link?

bashy's avatar

Actually, you probably have another issue with middleware redirecting them to login since it's trying to show a auth required page?

ha_ko2008's avatar

do whatever you do, your problem is that the previous URL is the login page, so even you solve the previous URL handling, so you will get back to the login page and sense you are logged in automatically you will be redirected to the '/' URL (see the file app\Http\Middleware\RedirectIfAuthenticated.php Line°: 21 `if (Auth::guard($guard)->check()) { return redirect('/'); )

i have solved it by saving session value and recall it like this :

1- Go to login blade page (resources\views\auth\login.blade.php) put this code in :

<?php
                        
  Session::set('backUrl', URL::previous());
?>

2- Then go to (this for logout action)(vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php) lock look for function logout and change its return to redirect()->back()


<?php
                        
  public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->flush();

        $request->session()->regenerate();

        return redirect()->back();
    }
?>

3- After that go to (vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php) first you should use Session; just change the redirectPath function its return


return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';

to


public function redirectPath()
    {

        
        return Session::get('backUrl') ? Session::get('backUrl') : $this->redirectTo;
        
    }


i didn't say it's the perfect solution but after a long way looking, i just invented it

Please or to participate in this conversation.