Laravel 4中的Auth::attempt()始终返回false

9

我将尝试使用Laravel的Auth类,但每次我尝试登录用户时,该方法会返回false。以下是我的代码:

Routes.php

Route::get('new-user', function() {
    return View::make('register');
});

Route::post('new-user', function() {
    $name = Input::get('name');
    $email = Input::get('email');
    $password = Hash::make(Input::get('password'));

    $user = new User;
    $user->name = $name;
    $user->email = $email;
    $user->password = $password;

    $user->save();
});    

Route::get('login', function() {
        return View::make('login');
    });

    Route::post('login', function() {

        $user = array(
            'email' => Input::get('email'),
            'password' => Hash::make(Input::get('password'))
        );

        if (Auth::attempt($user)) {
            //return Redirect::intended('dashboard');
            return "ok.";
        } else {
            return "Wrong.";
        }

    });

views/login.blade.php

{{ Form::open(array('url' => 'login', 'method' => 'post')) }}

    <h1>Login:</h1>

    <p>
        {{ Form::label('email', 'Email: ') }}
        {{ Form::text('email') }}<br />

        {{ Form::label('password', 'Password: ') }}
        {{ Form::password('password') }}<br />
    </p>

    <p>
        {{ Form::submit('Login') }}
    </p>

{{ Form::close() }}

config/auth.php

return array(

    'driver' => 'eloquent',
    'model' => 'User',
    'table' => 'users',
    'reminder' => array(
        'email' => 'emails.auth.reminder', 'table' => 'password_reminders',
    ),

);

这个数据库有电子邮件和密码字段,密码字段是varchar(60)。 每当我向/login发送登录信息时,它会返回"错误"。 我真的看不出问题在哪里?

5个回答

6

你的代码出错是因为你向Auth::attempt()传递了错误的变量。该方法需要一个数组,其中包含键值为username、password和可选的remember。因此,你的上述代码应该是:

Route::post('login', function()
{
    $credentials = [
        'username' => Input::get('email'),
        'password' => Input::get('password')
    ];

    dd(Auth::attempt($credentials));
});

希望这些能帮到您。 此外,我会给您一些额外的代码片段来改善您的工作流程。新用户存储路由:
Route::post('register', function()
{
    $input = Input::only(['username', 'email', 'password']);

    // validate data

    Eloquent::unguard();

    $user = User::create($input);

    Auth::loginUsingId($user->id);

    return Redirect::to('dashboard');
});

然后在你的用户模型中添加该方法

public function setPasswordAttribute()
{
    $this->password = Hash::make($this->password);
}

这样设置密码时,每次都会自动进行哈希处理。


5

尝试之前不要哈希密码:

    $user = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    if (Auth::attempt($user)) {
        //return Redirect::intended('dashboard');
        return "ok.";
    } else {
        return "Wrong.";
    }

它仍然返回false(“错误”)。 - John
1
根据文档(http://four.laravel.com/docs/security#storing-passwords),密码在传递给attempt()函数之前未经过散列处理。我确定这是一个问题,但你可能还有其他问题。请确保密码已正确存储在用户表中。 - Antonio Carlos Ribeiro
以下是我如何在数据库中存储用户的代码: $name = Input::get('name'); $email = Input::get('email'); $password = Hash::make(Input::get('password')); $user = new User; $user->name = $name; $user->email = $email; $user->password = $password; $user->save(); });``` 抱歉,我不知道怎么在这里正确展示代码。 - John
1
是的,安东尼奥是正确的。当您创建用户帐户时,必须在保存之前对密码进行哈希处理。在使用Auth::attempt时,请使用'password' => Input::get('password')。Laravel将对其进行哈希处理,并将哈希输入的密码与哈希存储的密码进行比较。任何其他组合的哈希/未哈希都将失败。 - Andrew Koper

1

这不起作用是因为auth :: attempt使用bcrypt将密码转换为哈希值,并在用户表中查找该哈希值进行匹配。

简而言之,密码应该是存储在数据库表中的哈希值,才能使auth :: attempt正常工作。

这就是为什么您的if()条件失败的原因。

您可以使用bcrypt(password)将密码存储为哈希值,然后使用auth :: attempt

以下内容来自laravel文档

https://laravel.com/docs/5.2/authentication#authenticating-users

尝试方法接受一个键值对数组作为其第一个参数。数组中的值将用于在您的数据库表中查找用户。因此,在上面的示例中,将通过电子邮件列的值检索用户。如果找到用户,则将比较存储在数据库中的哈希密码与通过数组传递给方法的哈希密码值。如果两个哈希密码匹配,则将为用户启动经过身份验证的会话。
如果认证成功,尝试方法将返回true。否则,将返回false。

0
您应该在模型类中实现 Laravel 提供的 UserInterface 类:

UserInterface

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{

请记住,它有两个抽象方法,您应该在模型中声明。您可以参照原始的User.php模型。


0

请检查您的密码长度。它必须在数据库中为60或更高。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接