繁体   English   中英

Laravel 4中的登录身份验证失败

[英]Login authentication fails in laravel 4

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

您应该对密码进行哈希处理。

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

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

当您要求Auth类尝试登录时,您传入用户名并按原样传递。 但是,如果您研究此方法,它将首先对密码进行哈希处理以使其安全,然后将其与数据库条目进行匹配。 在存储它时,从您当前的实现中,不会对其进行哈希处理。

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

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

尽管我不太确定您使用种子的方式在语法上是否正确,但是如果可行,只需在此处哈希密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM