Laravel身份验证因Auth :: attempt而失败

时间:2014-04-09 02:25:35

标签: php html authentication laravel laravel-4

我正在尝试Laravel 4上的Jeffrey Way Authentication Essentials教程,但我的结果并不相同。 (https://www.youtube.com/watch?v=msEwmVZ4wp4

当我检查dd($ attempt)时,即使我使用正确的凭据登录,也总是会出错。

请有人告诉我,我哪里出错了。我已经谷歌了很多,有些人声称,密码需要进行哈希处理,并在将其放入数据库之前对其进行哈希处理,但在其他地方仍然存在错误。

这是我的代码: 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');
}

以下是我的数据库的屏幕截图:http://i.imgur.com/sTGQu39.jpg

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

请有人纠正我的代码:)

亲切的问候,

乔治

2 个答案:

答案 0 :(得分:1)

问题实际上是密码的散列。

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

 // 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')
        ]);
    }
}

 }

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

答案 1 :(得分:1)

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

为了使你的代码有效,在注册用户时你应该在存储密码之前使用Hash :: make函数:

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

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

相关问题