简体   繁体   中英

How to redirect back after logged-in to previous page after logged out on that page in Laravel 5

I am using Laravel 5.3 , I have a situation here...I'm on PAGE-2 and logged-out from there then redirected to Login Page. Now, what I am trying to achieve is to redirect back to PAGE-2 if the user logs-in again.

the Current situation is that, the user will be redirected to defaultAfterLogin page, which is not my desired login flow.

NOTE: Default page after-login is " DashBoard ".

It is OKAY IF YOU WILL GO THE FIRST-TIME TO PAGE-2(not the default DashBoard page) and if you're not LOGGED-IN you will be redirected to LOGIN PAGE then IF YOU'll login again you will be redirected back to PAGE-2, which is fine.

BUT what is happening now is that, when you're in PAGE-2 then you LOGOUT then you will be REDIRECTED TO LOGIN-PAGE, if you LOGIN again you will be redirected to "DashBoard" which is not what I want. It should redirect back to PAGE-2

The flow should be,... users will be redirected after login no matter which PAGE they're previously working with.

Here's the a sample script I am using (it's actually from laravel i'am using its built-in Auth)

protected function handleUserWasAuthenticated(Request $request, $throttles)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }

        if (method_exists($this, 'authenticated')) {
            return $this->authenticated($request, Auth::guard($this->getGuard())->user());
        }

        return redirect()->intended($this->redirectPath());
    }

Any ideas, please? Thank you very Much for your help.

Try this On auth middleware:app/http/middleware/RedirectIfAuthenticated

// redirect the user to your login page "/login"

public function handle($request, Closure $next)
{
  if ($this->auth->check()) {
    return redirect('/login');
  }

  return $next($request);
}

// This is your login method

public function postSignIn(Request $request)
  { 
    $request_data = $request->all();
    $email = $request_data['email'];
    $user_details = User::where('email', $email)->first();
    if(count($user_details) > 0)
    {
      $credentials = array('email'=> $email ,'password'=> $request_data['password']);        
      if ($this->auth->attempt($credentials, $request->has('remember')))
      {
        return redirect()->to('/dashboard'); //Here is your redirect url, redirect to dashbord
          OR
        return redirect()->to('/page2'); //Here is your redirect url, redirect to page2
      }
      else
      {
        $error = array('password' => 'Please enter a correct password');
        return redirect()->back()->withErrors($error); 
      }
    }
    else
    {
      $error = array('password' => 'User not found');
      return redirect()->back()->withErrors($error);  
    }
  }

Open AuthController class : app/Http/Controllers/Auth/AuthController.php

Add below property to the class

protected $redirectAfterLogout = 'auth/login';

you can change auth/login with any url.

U CAN USE THIS CODE

       return redirect()->back();

or

u can use route and in ur route u need to configure and in controller u can put the below code // this is ur route

Route::get('/dashboard',[
'uses' => 'PostController@dashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);

//put this in ur controller return redirect()->route('dashboard');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM