2

在 Laravel4 中,我在路由中编写了以下代码,但它总是将我重定向到登录页面。我已经用谷歌搜索并在堆栈溢出时发现它并尝试了所有解决方案但没有成功。我相信这将是一个愚蠢的错误,但请找出它。谢谢

路线:

Route::post('login', function ()
    {
            $user = array(
        'username' => Input::get('username'),
        'password' => Hash::make(Input::get('password'))
    );
            /* Store entered username and password in an array named as 'user' */
            print_r($user);

            if (Auth::attempt($user)) 
            {
                return Redirect::route('home')->with('flash_notice', 'You are successfully logged in.');
                /* Authentication Success!!..Redirect user to home page */
            }
            else
            {
                return Redirect::route('login')
                    ->with('flash_error', 'Your username/password combination was incorrect.')->withInput();
                            /* Authentication failure!! lets go back to the login page */
            }
    });

用户模型:

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface 
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    // public $timestamps = false;

    /**
     * The primary key of the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }



/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */

}

用户播种机:

<?php
class UserSeeder extends Seeder {

    public function run()
    {
         DB::table('users')->delete();
        return array('table'=>'users',
        array(
                'username' => 'admin',
                'password' => 'admin'
         ),
        );
    }
}
4

2 回答 2

0

你应该散列你的密码。

array(
    'username' => 'admin',
    'password' => Hash::make('password')
),

您可以在文档中找到更多信息。

于 2013-08-24T13:42:37.613 回答
0

当您要求 Auth 类尝试登录时,您传入用户名并按原样通过。但是,如果您查看该方法,它将首先对密码进行哈希处理以使其安全,然后将其与数据库条目匹配。当您存储它时,从您当前的实现来看,它没有被散列。

如上所述,您应该在播种机中进行此更改:

array(
    'username' => 'admin',
    'password' => Hash::make('password')
),

虽然我不太确定您使用播种机的方式在语法上是否正确,但如果它有效,只需在那里散列密码即可。

于 2013-08-24T19:08:27.110 回答