I\'m working on my Laravel Project and trying to override the default postLogin() from AuthenticatesAndRegistersUsers . So I have updated my AuthController and added this t
I would add following first thing in postLogin() function.
$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 user table. 0 = Inactive, 1 = active. so whole function would look like following..
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',
]);
}
With
if ($this->auth->attempt($credentials, $request->has('remember')))
you are loggin the user in so if you want to log him out use
Auth::logout();
use that piece of code in the else if statement
Try following this discussion at laracasts.
Here is a solution
if ($this->guard()->validate($this->credentials($request))) {
$user = $this->guard()->getLastAttempted();
if ($user->is_activated && $this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
} else {
$this->incrementLoginAttempts($request);
if ($request->ajax()) {
return response()->json([
'error' => 'This account is not activated.'
], 401);
}
}
}
It provided a crucial update for a do my homework service at Top Homework Expert