1

I know this is a pretty generic title but after reading all the ones that come up, none of them mention the issue I'm having. Basically, I'm running into issues regarding the use of Auth::attempt(). I've checked $credentials to make sure I was using the right password and indeed I was.

This doesn't work -

public function store(Request $request)
{
    $credentials = ['email' => $request->email, 'hash' => $request->password];
    if (Auth::attempt($credentials)) {
        return redirect()->home();
    }
}

But retrieving the hashed password from DB and using this works -

if (Hash::check('password123', $hashedPassword)) {
        echo "works";
}

Is there a way for me to check what part of Auth::attempt is causing invalid credentials?

Paul Chu
  • 1,249
  • 3
  • 19
  • 27

2 Answers2

0

From the manual: https://laravel.com/docs/5.4/authentication#authenticating-users

Notice you need to pass through 'password' and not 'hash'.

<?php 
public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

EDIT

Please see this answer from before - How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication

Looks like you should continue to pass 'password' through to Auth::attempt but you need to change your User model.

Now in User model (app/models/User.php) file you need to add the following function:

public function getAuthPassword() {
    return $this->hash; 
}
Community
  • 1
  • 1
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • Thanks for the help! Sadly, I changed my user model to that and I'm still getting the ErrorException in GenericUser.php line 56: Undefined index: password –  May 10 '17 at 18:53
  • For some reason I don't think it's reading my User::class, I took a huge leap and changed the password column to password instead of hash and now I'm getting ErrorException in GenericUser.php line 46: Undefined index: id when I have protected $primaryKey = 'player_id'; in my User model already! Any ideas? –  May 10 '17 at 21:26
  • Hmm. Not too sure what's happening there. There are a few people running into the GenericUser error - here's the first result https://laracasts.com/discuss/channels/laravel/laravel-52-multiple-table-authentification-primery-key-error – waterloomatt May 11 '17 at 12:17
  • Yep, saw that. Laravel is a pain. I even tried making a new User model, changed it in config/auth, same error. I'm very disappointed. –  May 11 '17 at 23:58
0

Use this code

$password = "test";

$hash = '$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e';



if (password_verify($password, $hash)) {
    echo "Success";
}
else {
    echo "Error";
}
Brijesh Dubey
  • 118
  • 2
  • 12