Laravel 4验证::尝试()总是返回false [英] Laravel 4 Auth::attempt() always returns false

查看:403
本文介绍了Laravel 4验证::尝试()总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了Laravel的验证类,但每次我尝试登录用户,该方法返回false。这里是我的code:

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:

routes.php文件

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.";
        }

    });

意见/ login.blade.php

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() }}

配置/ auth.php

config/auth.php

return array(

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

);

该数据库具有电子邮件和放大器;密码字段和密码字段为varchar(60)。
每当我送的登陆信息/登录返回我的错。
我实在看不出什么错在这里?

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?

推荐答案

您code是窃听,因为您传递了错误的变量验证::尝试()。该方法需要与钥匙的用户名,密码和可选记住一个数组。有鉴于此,你上面code应该是:

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:

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

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

希望有所帮助。

此外,我会给你额外的code段,以提高您的工作流程。路径来存储新用户:

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

这篇关于Laravel 4验证::尝试()总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆