Created
March 14, 2013 01:16
-
-
Save msurguy/5158026 to your computer and use it in GitHub Desktop.
Laravel redirect to last visited page if not logged in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::filter('auth', function() | |
{ | |
if (Auth::guest()) | |
{ | |
// Save the attempted URL | |
Session::put('pre_login_url', URL::current()); | |
// Redirect to login | |
return Redirect::to('login'); | |
} | |
}); | |
Route::post('login', function() | |
{ | |
// Get the POST data | |
$data = array( | |
'username' => Input::get('username'), | |
'password' => Input::get('password') | |
); | |
// Attempt Authentication | |
if ( Auth::attempt($data) ) | |
{ | |
// If user attempted to access specific URL before logging in | |
if ( Session::has('pre_login_url') ) | |
{ | |
$url = Session::get('pre_login_url'); | |
Session::forget('pre_login_url'); | |
return Redirect::to($url); | |
} | |
else | |
return Redirect::to('admin'); | |
} | |
else | |
{ | |
return Redirect::to('login')->with('login_errors', true); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment