zoldic's avatar

How to redirect back to previous page after login success?

Hi, do you guys know how to redirect back to previous page after login success? There is an answer for it at stackoverflow but I'm kinda confuse about which file he is refering to https://stackoverflow.com/questions/15389833/laravel-redirect-back-to-original-destination-after-login

answered by vFragosop

Simply put,

On auth middleware: (what is the file name he is refering to? RedirectIfAuthenticated.php?)

// redirect the user to "/login"
// and stores the url being accessed on session
if (Auth::guest()) {
    return redirect()->guest('login');
}
return $next($request);

On login action: (same here, what is the file name? LoginController.php?)

// redirect the user back to the intended page
// or defaultpage if there isn't one
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('defaultpage');
}
0 likes
9 replies
tisuchi's avatar

Use-

return redirect()
        ->back();

Normally, after successfully logged in, the best way to redirect to protected page is intended().

5 likes
zoldic's avatar

in the LoginController.php there is a line of code that control this login success redirect protected $redirectTo = '/home';

where should I put this into? what is file name?

return redirect()
        ->back();

and this one, where should I put it? what is the file name?

return redirect()->intended('defaultpage');
zoldic's avatar

Hi I have watch this video from Laravel 5.4 From Scratch (But Jeffrey didn't really teach about it)

  • Rapid Authentication and Configuration
  • Associating With Users
  • Associating With Users: Part 2

(I use Lav 5.4) I have check my LoginController.php, but i can't find this line of code

if (Auth::attempt(['email' => $email, 'password' => $password])) {

and I simply put your code to my LoginController.php and it crashed

My LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    
    protected $redirectTo = '/home';
    
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function credentials(Request $request) {
        return $request->only($this->username(), 'password') + ['verified' => true];
    }
}
1 like
bashy's avatar

@tisuchi Little tip, these do the same thing :)

return back();

// same as

return redirect()->back();
3 likes
zoldic's avatar

Hi tisuchi, any help man? I still can't figure it out

tisuchi's avatar

Go to app/Http/Controllers/Auth/AuthController.php.

You will get something like this-

<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

Now, in this last line of my code, you can see-

    protected $redirectTo = '/';

Here, you can change where ever you want to redirect after successfully login. It can be like this-

protected $redirectTo =  'your-url';
3 likes
zoldic's avatar
zoldic
OP
Best Answer
Level 1
protected $redirectTo =  'your-url';

Yes, I understand that line of code but the problem is that code is static, you can only redirect back to your predifined url It can't redirect back to previous page

and I found this solution by Scott Byers, but it is not working too

Laravel >= 5.3 The Auth changes in 5.3 make implementation of this a little easier, and slightly different than 5.2 since the Auth Middleware has been moved to the service container. Modify the new Middleware auth redirector

/app/Http/Middleware/RedirectIfAuthenticated.php

Change the handle function slightly, so it looks like:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect()->intended('/home');
    }

    return $next($request);
}

(Solution) I managed to solve it (but I'm not that sure if this is the correct way to do it) in the AuthenticatesUsers.php file

    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        return back(); //add this line of code
    }

and btw I found this RedirectsUsers.php file, but I have no idea how it work so I don't touch it

<?php

namespace Illuminate\Foundation\Auth;

trait RedirectsUsers
{
    /**
     * Get the post register / login redirect path.
     *
     * @return string
     */
    public function redirectPath()
    {
        if (method_exists($this, 'redirectTo')) {
            return $this->redirectTo();
        }

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

Please or to participate in this conversation.