skip to Main Content
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


  1. 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:

    class User extends Authenticatable
    {
       ...
    
          protected $casts = [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }
    

    This would allow you to create users as simply as:

    $user = new User();
    $user->username = '[email protected]';
    $user->password = 'supersecretpassword';
    $user->save();
    

    With the password hashed in your DB, you can use Laravel’s Auth helper to authenticate your users:

    public function login(Request $request)
    {
        // Validate the request
        $request->validate([
            'username' => 'required|string',
            'password' => 'required|string',
        ]);
    
        if (Auth::attempt($credentials)) {
             $request->session()->regenerate();
     
             return redirect()->intended('/');
         }
    
        // Authentication failed, return an error response
        return response()->json(['error' => 'Invalid credentials'], 401);
    }
    

    Laravel auth docs: https://laravel.com/docs/10.x/authentication#authenticating-users

    Login or Signup to reply.
  2. 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 to appconfigapp.php. for more information access laravel hashing

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search