Quantcast
Channel: Login only if user is active using Laravel - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by sk8gear for Login only if user is active using Laravel

$
0
0

Probably not the best but, I think I found a cleaner way to override the login method. I tried this on Laravel 7

in Auth\LoginController.php, put these classes

use Illuminate\Support\Facades\Auth;use Illuminate\Validation\ValidationException;

and then, add(override) these functions inside LoginController class:

public function login(Request $request){            $this->validateLogin($request);    if (method_exists($this, 'hasTooManyLoginAttempts') &&        $this->hasTooManyLoginAttempts($request)) {        $this->fireLockoutEvent($request);        return $this->sendLockoutResponse($request);    }    if($this->guard()->validate($this->credentials($request))) {        // Check if user is active, else return error message        if(Auth::attempt(['email' => $request->email, 'password' => $request->password, 'status' => 'A'])) {            return redirect()->intended('dashboard');        }  else {            // This will return the message required as desired            return $this->inactiveCredential($request);        }    } else {                    $this->incrementLoginAttempts($request);        return $this->sendFailedLoginResponse($request);    }}// Error massage for inactive credentialprivate function inactiveCredential(Request $request){        throw ValidationException::withMessages([        // auth.not-active can be added in resources/lang/en/auth.php        $this->username() => [trans('auth.not-active')],    ]);    }

Then add this line in resources/lang/en/auth.php. If there are more than 1 language, you should put this line in there too.

'not-active' => 'This account is already deleted. Contact administrator to revoke this account',

Then you should have this response on the default laravel-ui login interfaceLogin Credential


Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>