Laravel身份验证失败,Auth::尝试


Laravel Authentication fails with Auth::attempt

我正在Laravel 4上试用Jeffrey Way Authentication Essentials教程,但结果不一样。(https://www.youtube.com/watch?v=msEwmVZ4wp4)

当我检查dd($try)时,即使我使用正确的凭据登录,我也总是得到false。

请有人告诉我,我哪里错了。我已经在谷歌上搜索了很多,有些人声称,在将密码放入数据库之前,需要对其进行哈希处理,我对其进行了哈希处理,但其他地方肯定还是有错误。

这是我的代码:http://help.laravel.io/faff81e66d3672cb96d5ae2f8d0cccbf2e7f9052

这里还有最重要的代码:

我的视图格式:create.blade.php

登录

{{ Form::open(array('route' => 'sessions.store')) }}
    <ul>
        <li>
            {{ Form::label('email', 'Email:')}}
            {{ Form::text('email')}}
        </li>
        <li>
            {{ Form::label('password', 'Password:')}}
            {{ Form::password('password')}}
        </li>
        <li>
            {{ Form::submit() }}
        </li>
    </ul>
{{ Form::close() }}

和我的SessionsController.php

public function create()
{
    //
    $users = User::all();
    return View::make('sessions.create')
        ->with('users', $users);
}
/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    // validate
    $input = Input::all();
    $attempt = Auth::attempt([
        'email' => $input['email'],
        'password' => $input['password']
    ]);
    dd($attempt);
    // if($attempt) {
    //  return Redirect::intended('/');
    // } else {
    //  return 'whatever bro';
    // };
    // dd('problem');
}

这是我的数据库截图:https://i.stack.imgur.com/BeqcH.jpg

如果我输入正确的登录凭据,我希望dd($try)的布尔值是正确的,但它总是显示为false。

请有人纠正我的代码:)

问候,

George

从数据库的屏幕截图中,我可以看到您在将密码存储到数据库之前没有对其进行哈希处理。Auth::attempt函数认为您的密码是散列的,因此它将Hash::make应用于给定的密码,然后将其与数据库中存储的密码进行比较。

为了使您的代码正常工作,在注册用户时,您应该在存储密码之前使用Hash::make函数:

 $password = Hash::make($input['password']);
 $user->password = $password;
 $user->save(); 

查看文档http://laravel.com/docs/security

问题实际上是密码的哈希。

对于任何遇到同样问题的人,请将您的种子更改为:

 // Composer: "fzaninotto/faker": "v1.3.0"
 use Faker'Factory as Faker;
 class UsersTableSeeder extends Seeder {
public function run()
{
    $faker = Faker::create();
    foreach(range(1, 10) as $index)
    {
        User::create([
            'username' => $faker->name,
            'email' => $faker->email,
            'password' => Hash::make('1234')
        ]);
    }
}
 }

在之前的尝试中,我只是尝试播种,但没有以上述方式播种。尽管如此如果有帮助的话,希望有人能更幸运地找到这个答案。