【问题标题】:Laravel 5.4 Authentication Auth Attempt returning falseLaravel 5.4 Authentication Auth 尝试返回 false
【发布时间】:2017-04-28 01:54:54
【问题描述】:

我知道这是一个非常笼统的标题,但在阅读了所有出现的标题后,没有一个提到我遇到的问题。基本上,我遇到了有关使用Auth::attempt() 的问题。我检查了$credentials 以确保我使用了正确的密码,而且确实如此。

这行不通-

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

但是从数据库中检索散列密码并使用它是可行的 -

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

我有没有办法检查Auth::attempt 的哪一部分导致凭据无效?

【问题讨论】:

  • 将哈希更改为密码。 '密码' => $request->密码
  • hash 是我表中的列名吗?

标签: php authentication laravel-5.4


【解决方案1】:

来自手册:https://laravel.com/docs/5.4/authentication#authenticating-users

请注意,您需要通过“密码”而不是“哈希”。

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

编辑

请看之前的回答 - How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication

看起来您应该继续将“密码”传递给 Auth::attempt,但您需要更改您的用户模型。

现在在用户模型 (app/models/User.php) 文件中,您需要添加 以下功能:

public function getAuthPassword() {
    return $this->hash; 
}

【讨论】:

  • 感谢您的帮助!可悲的是,我将我的用户模型更改为那个,但我仍然在 GenericUser.php 第 56 行收到 ErrorException:未定义索引:密码
  • 出于某种原因,我认为它没有读取我的 User::class,我进行了一次巨大的飞跃并将密码列更改为密码而不是哈希,现在我在 GenericUser.php 中收到 ErrorException第 46 行:未定义索引:当我保护 $primaryKey = 'player_id' 时的 id;在我的用户模型中!有什么想法吗?
  • 嗯。不太清楚那里发生了什么。有几个人遇到了 GenericUser 错误 - 这是第一个结果 laracasts.com/discuss/channels/laravel/…
  • 是的,看到了。 Laravel 很痛苦。我什至尝试制作一个新的用户模型,在 config/auth 中更改它,同样的错误。我很失望。
【解决方案2】:

使用此代码

$password = "test";

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



if (password_verify($password, $hash)) {
    echo "Success";
}
else {
    echo "Error";
}

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 2019-02-04
    • 2018-12-19
    • 2015-03-17
    • 1970-01-01
    • 2017-05-11
    • 2017-11-27
    • 2019-06-28
    • 2018-01-07
    相关资源
    最近更新 更多