Reputation: 2895
I had a functioning user log in route in my controller however the method itself was very long and handled multiple issues such as the possibility of the user being banned, ip logging etc. I wanted to break the method down so that it was more managable and I was wondering if this is possible? Here are the 3 methods where 'postLogin()' is the route:
public function postLogin()
{
$validator = Validator::make(Input::all(), array(
'login-username' => 'required',
'login-password' => 'required'
));
if($validator->fails())
return Redirect::back()->withErrors($validator)->withInput();
else
{
$user_attempt = Input::get('login-username');
$pass_attempt = Input::get('login-password');
$auth = Auth::attempt(array(
'username' => $user_attempt,
'password' => $pass_attempt
), true);
if($auth)
$this->handleAuth();
else
return Redirect::route('home')->with('fail', 'Incorrect username or password, username: ' . $user_attempt . ' pass: ' . $pass_attempt);
}
}
private function handleAuth()
{
$username = Auth::user()->username;
$banned_info = UserBans::getBanned($username);
$stored_ip = Auth::user()->ip_address;
$current_ip = User::getIp();
if($stored_ip != $current_ip)
User::updateIp(Auth::user(), $current_ip);
if(is_null($banned_info))
return Redirect::intended('/');
else
$this->handleBan($banned_info, $current_ip);
}
private function handleBan($banned_info, $current_ip)
{
if(UserBans::isBanned($banned_info['ban_end']))
{
$message = "This account is currently banned until: " . $banned_info['ban_end'] . " Reason: " . $banned_info['reason'];
if($banned_info['ip_address'] != $current_ip)
{
$banned_model = UserBans::getBannedModel($banned_info['id']);
User::updateIP($banned_model, $current_ip);
}
Auth::logout();
return Redirect::route('home')->with('fail', $message);
}
else
{
UserBans::destroy($banned_info['id']);
return Redirect::intended('/');
}
}
The issue I'm finding is that the the main controller method will call the helper methods with no problem however the helper methods attempt to redirect to routes for example in handleAuth():
if(is_null($banned_info))
return Redirect::intended('/');
This occurs if the user is not banned and has the correct credentials, normally it would redirect to the home page and you would be logged in, however when this method calls the intended I am left with a blank page at the 'postLogin' routes url. If you refresh the page you are at the home and are logged in. Here are the relevant routes:
Route::group(array('before' => 'guest'), function()
{
Route::group(array('before' => 'csrf'), function()
{
Route::post('/user/login', array('uses' => 'UserController@postLogin', 'as' => 'postLogin'));
});
});
Is this possible to do with laravel routing/controllers? if not can you give any recommendations on how to handle this situation?
Upvotes: 0
Views: 2295
Reputation: 500
at handleAuth()
, return Redirect::intended('/');
is returning something to postLogin()
. You need to return that value from the postLogin()
.
So, add return
at postLogin()
.
if($auth)
return $this->handleAuth();
Other Fixes
at handleAuth()
, also add return
else
return $this->handleBan($banned_info, $current_ip);
Upvotes: 1
Reputation: 3588
Looks you are forgot to return handleBan() results
public function postLogin()
{
//...
if($auth)
return $this->handleAuth();
//...
}
private function handleAuth()
{
//...
if(is_null($banned_info))
return Redirect::intended('/');
else
return $this->handleBan($banned_info, $current_ip);
}
Upvotes: 1