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

时间:2016-09-09 16:36:09

标签: php laravel laravel-5 laravel-5.2 laravel-5.1

被修改

  • Laravel版本:5.2.45
  • PHP版本:5.6
  • 数据库驱动程序&版本:

说明

嗨,

我完成了从laravel 5.1到5.2的更新并解决了一些问题......

现在我尝试登录,但是Auth验证了'password'字段的密码,但在我的db中,密码的另一个名字是'senha'。

照亮/认证/ 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();
    }

你怎么看,该功能尝试在凭证中找到字符串'password'时应该是'senha'

与文件中的validateCredentials函数相同的问题是照亮/​​ 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());
    }

解决此问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

最好的方法是......

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

在他们的模型身份验证中:

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

答案 1 :(得分:0)

正如Dayglor指出的那样,EloquentUserProvider不会查询密码字段。它希望凭据始终具有“密码”字段,其他任何字段都是用于查询数据库的字段。只有在找到用户之后才能通过hash_check检查密码。需要credentials['password]$user->getAuthPassword()进行哈希检查。因此,只需在User模型上设置getAuthPassword方法,即可返回要用作要检查的password字段的正确字段。