更新了Laravel 5.2 - Auth始终返回false

Edited

#
  • Laravel Version: 5.2.45
  • PHP Version: 5.6
  • Database Driver & Version:

Description:

Hi,

I finished update from laravel 5.1 to 5.2 and resolved some problems...

Now i try make login, but the Auth verify the password of the 'password' field but in my db the password has another name that is 'senha'.

illuminate/Auth/EloquentUserProvider.php

/**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials)) {
            return;
        }

        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value) {
            **if (! Str::contains($key, 'password'))** {
                $query->where($key, $value);
            }
        }

        return $query->first();
    }

how you see, the function tried find the string 'password' in the credentials when it should be 'senha'

The same problem with the validateCredentials function in file illuminate/Auth/EloquentUserProvider.php

/**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  array  $credentials
     * @return bool
     */
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

What is the best way to fix this?

The best way is ...

\Auth::attempt( [ 'email' => ' campos@b.com.br ' , 'password' => ' 1234567890 '] );

And in their model authentication :

/ **
     * Get the password for the user .
     *
     * @return String
     * /
    getAuthPassword public function ( )
    {
        return $this->senha;
    }

As Dayglor was pointing out, the EloquentUserProvider doesn't query for the password field. It expects the credentials to always have a 'password' field, any other fields are what it uses to query the database. Only after it finds a user will it then check the password by doing a hash_check. It takes the credentials['password] to hash check against $user->getAuthPassword(). So just set on your User model the getAuthPassword method to return the correct field you want to be used as the password field to check against.