'I want to show a specific message when the user fail to login for username and password separately in laravel 5
I am using laravel login api.
I want to show a message for users who fail to login whether it's the username that is not correct or the password the right way.
I looked at AuthenticatesUsers.php there is a method that is responsible for returning a failed login message called sendFailedLoginResponse I edited it so it can do what I want and that's my code.
protected function sendFailedLoginResponse(Request $request)
{
$inputemail = $request->input('email');
$user = User::where('email', $inputemail)->first();
if($user == '') {
throw ValidationException::withMessages([
$this->username() => [trans('auth.usernameWrong')],
]);
} else {
throw ValidationException::withMessages([
'password' => [trans('auth.passwordWrong')],
]);
}
}
I know that there is a better way than this becasue I doubly checked the username the first one was laravel check and the second is my query check.
I am new to laravel and I want to know how it can be done the right way.
Solution 1:[1]
Try this
protected function sendFailedLoginResponse(Request $request)
{
// Load user from database
$user = \App\User::where($this->username(), $request->{$this->username()})->first();
if(!Hash::check($request->password, $user->password) {
throw ValidationException::withMessages([
$this->username() => [trans('auth.passwordWrong')],
]);
} elseif($!user->exists){
throw ValidationException::withMessages([
'password' => [trans('auth.usernameWrong')],
]);
}
}
Use Hash::check() method to check the password
Solution 2:[2]
This should help
Go to vendor\laravel\ui\auth-backend\AuthenticatesUsers.php and edit the existing function sendFailedLoginResponse(Request $request)
protected function sendFailedLoginResponse(Request $request)
{
$user = User::where($this->username(), $request->{$this->username()})->first();
if($user && !Hash::check($request->password, $user->password) ){
throw ValidationException::withMessages([
'password' => [trans('auth.password')],
]);
}else{
throw ValidationException::withMessages([
$this->username() => [trans('auth.email')],
]);
}
}
Don't forget to include:
use App\Models\User;
use Illuminate\Support\Facades\Hash;
Make sure the above codes are placed inside the vendor\laravel\ui\auth-backend\AuthenticatesUsers.php
Now you can navigate to resources\lang\en\auth.php and include in the code below:
'email' => 'The provided email is incorrect.',
'password' => 'The provided password is incorrect.',
Solution 3:[3]
You can check the password with Hash::check() method:
$user = User::where('email', $request->email)->first();
if (is_null($user)) {
// There is no user
} elseif (!Hash::check($request->password, $user->password) {
// Password is wrong
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Iwalewa Fawaz |
| Solution 3 | Alexey Mezenin |
