How about this?
public function processLogin()
{
$validator = Validator::make(request()->all(), [
'email' => 'required|email',
'password' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$credentials = request()->only(['email', 'password']);
if(auth()->attempt($credentials)) {
if (Auth::user()->hasAnyRole('admin'))
alert()->success('Success', 'Admin logged in!')->toToast();
return redirect('/dashboard');
} else {
if (auth()->user()->email_verified_at === null) {
auth()->logout();
alert()->warning('Pending', 'Your account is not activated!')->toToast();
return redirect()->route('login');
}
}
alert()->success('Success', auth()->user()->name.' logged in!')->toToast();
return redirect()->intended();
}
Your original code has some issues with the closing brackets and unreachable return statements. I'm surprised your IDE isn't squawking about it. Please note, I removed the final return statement as it was unreachable and redundant.
Also take a look at the below if/else statement from your example. Essentially you are saying, if the credentials are valid and the user is an admin, then send them to /dashboard.
if(auth()->attempt($credentials)) {
if (Auth::user()->hasAnyRole('admin'))
alert()->success('Success', 'Admin logged in!')->toToast();
return redirect('/dashboard');
} else {
if (auth()->user()->email_verified_at === null) {
auth()->logout();
alert()->warning('Pending', 'Your account is not activated!')->toToast();
return redirect()->route('login');
}
}
But if the credentials are invalid and the user's email as not yet been verified, then redirect them to the login page with the message that the account has not been activated. But then, and this is important, if the credentials are not valid, the following block is executed.
alert()->success('Success', auth()->user()->name.' logged in!')->toToast();
return redirect()->intended();
I guess that's why you originally had the final return statement, below, in the original code. But I'm pretty sure that was unreachable in the structure you had used.
alert()->error('Invalid credentials', 'Your Email or password was incorrect!');
return redirect()->back();
Anyway, good luck with the project!