public function login(Request $request)
{
// Validate the request
$request->validate([
'username' => 'required|string',
'password' => 'required|string',
]);
// Attempt to find the user by username
$user = User::where('username', $request->username)->first();
// Check if the user exists
if (!$user) {
return response()->json(['error' => 'Invalid credentials'], 401);
}
// Check if the password is correct
if (Hash::check($request->password, $user->password)) {
// Authentication successful, login the user
Auth::login($user);
// Redirect to the desired route
return redirect()->intended('/');
}
// Authentication failed, return an error response
return response()->json(['error' => 'Invalid credentials'], 401);
}
this is the code that i use
enter image description here this the data
enter image description here i try to login thenenter image description here this happen help me pls
i try ask all code ai but still cant solve the problems im newbie btw
2
Answers
As others have mentioned, your hash checks are failing because you’re storing the passwords in plaintext. This is absolutely not the way to do it.
If you’re using Laravel’s default User model, it should cast passwords to a hash when you create a user. Normally, your User model would look like this:
This would allow you to create users as simply as:
With the password hashed in your DB, you can use Laravel’s Auth helper to authenticate your users:
Laravel auth docs: https://laravel.com/docs/10.x/authentication#authenticating-users
This error is caused by the verification of the hash you use for encryption not being the same as the Laravel system. If you use a special type of encryption in Laravel, look at the file
appconfighashing.php
within your application, there you can configure if you want verification by bcrypt and change the hasing information in addition toappconfigapp.php
. for more information access laravel hashing