0

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.

Emz
  • 502
  • 1
  • 3
  • 11
  • You may find the answer here http://stackoverflow.com/questions/15389833/laravel-redirect-back-to-original-destination-after-login – Dev Sep 19 '16 at 05:16
  • This doesn't answer my question. But I tried it still not working... by the way I'm using Laravel's Login Authentication. – Emz Sep 19 '16 at 07:20

3 Answers3

1

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);  
    }
  }
Komal
  • 2,716
  • 3
  • 24
  • 32
  • I tried that but Not working... Please see sample code above .. I have edited my POST. – Emz Sep 19 '16 at 07:50
  • Best way is to create custom login, b'coz you can handle it in your own way and redirect anywhere. Plus you can do your other things after login like send confirmation mail. – Komal Sep 19 '16 at 08:54
  • My only problem is it redirects to the default page after logging out and login from Page-1,Page-2 and so on... But if logging-in for the first-time from any of those pages no problem it will redirect to specific page. But after logged-in for example and you're in Page-5 already and you Logout, when you login again it will redirect you to Default Page. Which is not I want .. it should redirect back to Page-5. – Emz Sep 19 '16 at 09:25
1

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.

Komal
  • 2,716
  • 3
  • 24
  • 32
0

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');

rupesh
  • 277
  • 3
  • 15