【问题标题】:Cannot authenticate session - laravel 4.2无法验证会话 - laravel 4.2
【发布时间】:2015-02-23 23:55:58
【问题描述】:
if(Auth::attempt(['username' => Input::get('username'), 'password' => Input::get('password')])){
    return Auth::user();
}
return 'Failed!';

这是有问题的代码,我确信问题出在Auth::attempt 行。我找到了关于这个的其他主题,但他们都没有解决方案。即:https://laracasts.com/forum/?p=1314-laravel-from-scratch-authentication-lesson-help/0

我的User 模型实现了UserInterface,并且我还将use Illuminate\Support\Facades\Auth as Auth; 放入控制器中,以防在Auth::attempt 部分中无法识别它。然而,无论有没有use ...,它都不起作用。

我也试过了

Auth::attempt(Input::only('username', 'password'))

而且我总是会到达Failed! 部分...

密码作为哈希字符串存储在数据库中,当我使用以下内容进行调试时:

echo Input::get('username') . "____" . Input::get('password');
die();

我得到了正确的结果。所以,除了::attempt函数中的问题,我想不出别的了。

有什么建议吗?

**更新:**User 模型

使用 Illuminate\Auth\UserTrait; 使用 Illuminate\Auth\UserInterface; 使用 Illuminate\Auth\Reminders\RemindableTrait; 使用 Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $fillable = [
        'username',
        'password',
        'email'
    ];

    public static $rules = [
        'username' => 'required',
        'password' => 'required',
        'email'    => 'required'
    ];

    public static $error_messages;

    /**
     * The database table used by the model.
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public static function isValid($input)
    {
        $validation = Validator::make($input, static::$rules);
        if ($validation->passes()) return true;

        static::$error_messages = $validation->messages();

        return false;
    }

【问题讨论】:

  • 你能告诉我们你的User模特吗?
  • @lukasgeiter - 已更新。 :)
  • @lukasgeiter - 我发现了问题并发布了答案。不过,谢谢你的尝试。
  • 太棒了!我要花很长时间才能猜到那个。我会将它添加到我脑海中的列表中,以解决 SO 上的下一个“auth is not working”问题;)

标签: authentication laravel laravel-4


【解决方案1】:

我找到了问题的原因:

有趣的是,它在数据库中。 password 列有 15 个字符的限制,而哈希编码的密码值有数十个字符。因此,从输入中获取的密码值的散列并没有完全保存在数据库中,而只是散列码的前 15 个字符。因此,去散列(如果我可以这样称呼它的话)从未成功。 :)

在我看来,这是一个非常愚蠢的错误,但很容易被发现......

【讨论】:

  • 顺便说一句:“请记住,在为此模型构建 Schema 时,请确保密码字段至少为 60 个字符。”,Official Docs
  • 我只是从 Laracasts 的视频教程和在线故障排除 + 研究中学习。我想我也必须阅读官方文档... :)
  • 没关系;)
猜你喜欢
  • 1970-01-01
  • 2015-04-27
  • 2014-10-27
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多