There are several similar questions but all of them seem incomplete as they are referring to not existing functions.
I am referring to:
Check for active user state with laravel
Login only if user is active using Laravel
extend laravel 5 built-in authentication to login only "if user == active"
In all of them there are presented solutions mostly to alter functions from AuthController
, however those functions are not there.
I am using latest version of Laravel (5.2) so my default mentioned file looks like: https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/AuthController.php
Now, how do I implement this functionality? I have tried copying public function postLogin()
(as suggested in those other mentioned posts) into that AuthController
file. Nothing changed.
I am clearly missing something here.
Please someone help!
Edit: The function that I have added is:
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $this->getCredentials($request);
// This section is the only change
if (Auth::validate($credentials)) {
$user = Auth::getLastAttempted();
if ($user->active) {
Auth::login($user, $request->has('remember'));
return redirect()->intended($this->redirectPath());
} else {
return redirect($this->loginPath()) // Change this to redirect elsewhere
->withInput($request->only('email', 'remember'))
->withErrors([
'active' => 'You must be active to login.'
]);
}
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}