php Laravel 4 Auth::attempt() 总是返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17111770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:17:49  来源:igfitidea点击:

Laravel 4 Auth::attempt() always returns false

phpauthenticationlaravel

提问by John

I'm trying the Laravel's Auth class but everytime i attempt to log in a user, the method returns false. Here's my code:

我正在尝试 Laravel 的 Auth 类,但每次我尝试登录用户时,该方法都会返回 false。这是我的代码:

Routes.php

路由.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

意见/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

配置/auth.php

return array(

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

);

The database has the email & password fields, and the password field is varchar(60). Whenever i send the login info to /login it returns me "Wrong." I really can't see whats wrong here?

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

回答by Neo Ighodaro

Your code is bugging out because you are passing the wrong variables to Auth::attempt(). That method requires an array with keys username, password and optionally remember. In that light, your above code should be:

您的代码出了问题,因为您将错误的变量传递给Auth::attempt(). 该方法需要一个包含用户名、密码和可选记住的键的数组。有鉴于此,您上面的代码应该是:

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

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

Hope that helps.

希望有帮助。

Also I'll give you snippets of extra code to improve your work flow. Route to store new user:

另外,我会给你一些额外的代码片段来改进你的工作流程。存储新用户的路由:

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

Then in your user model add the method

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

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

This way the password will be automatically hashed every time it's set

这样每次设置密码时都会自动散列

回答by Antonio Carlos Ribeiro

Don't hash the password before attempt:

在尝试之前不要散列密码:

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

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

回答by Laravel User

this will not work because auth::attempt converts password to hash using bcrypt, and looks for that hash in users table to match.

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

in short the password should be a hash stored in database table for auth::attempt to work.

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

that is why your if() condition failing.

这就是你的 if() 条件失败的原因。

you can use bcrypt(password) to store password as hash in database and then use auth::attempt

您可以使用 bcrypt(password) 将密码作为哈希存储在数据库中,然后使用 auth::attempt

below is from laravel docs

以下来自 Laravel 文档

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

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

The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. If the two hashed passwords match an authenticated session will be started for the user.

The attempt method will return true if authentication was successful. Otherwise, false will be returned.

尝试方法接受一个键/值对数组作为它的第一个参数。数组中的值将用于在数据库表中查找用户。因此,在上面的示例中,用户将通过 email 列的值进行检索。如果找到用户,存储在数据库中的散列密码将与通过数组传递给方法的散列密码值进行比较。如果两个散列密码匹配,将为用户启动经过身份验证的会话。

如果身份验证成功,尝试方法将返回 true。否则,将返回 false。

回答by revo

You should implement UserInterfaceclass provided by laravel within your model class:

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

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

class User extends Eloquent implements UserInterface, RemindableInterface
{

And remember that it has 2 abstract methods that you should declare at your model. You can follow original User.phpmodel

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

回答by Khalil Laleh

Check your password Length. It must be 60 or higher in database.

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