Laravel 5 redirect function
I am working on my Laravel project and is trying to override the default postLogin () from AuthenticatesAndRegistersUsers. So I updated my AuthController and added this to override the inline login,
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{
/* Check if the user is Activated */
$userID = \Auth::user()->id;
$user = new \App\User;
$result = $user->isUserActivated($userID);
if($result[0]->status == 1)
{
return redirect()->intended($this->redirectPath());
}
else if($result[0]->status == 0)
{
Session::flash('alert-danger', 'Your account is not yet Activated.');
return Redirect::to('auth/login');
}
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
As you can see, I have a $ result [0] -> status that tells if the user is activated, if not then I redirect them back to auth / login. I tried var_dump ($ result [0] -> status); and it works fine and also means that I override what it displays it but my problem is instead of redirecting it to auth / login it still goes through the house and can login even if the status is 0 It seems like this my override doesn't work, but when I var_dump $ result [0] -> status, it shows. Did I miss something?
source to share
I would add the following to the functions postLogin()
.
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors('Your account is Inactive or not verified');
}
status is a flag in the user table. 0 = Inactive, 1 = Active. so the whole function would look like this.
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors('Your account is Inactive or not verified');
}
$credentials = array('email' => $request->email, 'password' => $request->password);
if ($this->auth->attempt($credentials, $request->has('remember'))){
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => 'Incorrect email address or password',
]);
}
source to share