登录身份验证laravel 4失败 [英] Login authentication fails in laravel 4

查看:120
本文介绍了登录身份验证laravel 4失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel4,我写了下面的code的路线,但它总是我重定向到登录页面。
我用Google搜索,发现它的堆栈溢出过,并试图所有的解决方案,但不succeeded.I我相信这将是一个愚蠢的错误,但好心跟踪它out.Thank你

路线:

 路线::后('登陆',函数()
    {
            $ USER =阵列(
        用户名= GT;输入::获得(用户名),
        '密码'=>哈希::彩妆(输入::得到('密码'))
    );
            / *存放在名为用户的阵列输入用户名和密码* /
            的print_r($用户);            如果(AUTH ::尝试($用户))
            {
                返回重定向::路线(家) - GT;与('flash_notice','您成功登录。');
                / *认证成功!!重定向用户到主页* /
            }
            其他
            {
                返回重定向::路线(登录)
                     - >在('flash_error','您的用户名/密码组合是不正确的。) - GT; withInput();
                            / *认证失败!让我们回到登录页面* /
            }
    });

用户模型:

 < PHP使用照亮\\验证\\的UserInterface;
使用照亮\\身份验证\\提醒\\ RemindableInterface;类用户扩展了雄辩的UserInterface实现,RemindableInterface
{
    / **
     *模型所使用的数据库表。
     *
     * @var串
     * /
    保护$表='用户';    //公共$时间戳= FALSE;    / **
     *表中的主键。
     *
     * @var串
     * /
    保护$的PrimaryKey ='ID';    / **
     *从模型的JSON形式排除的特性。
     *
     * @var阵列
     * /
    保护的隐藏$ =阵列('密码');    / **
     *获取用户的密码。
     *
     * @返回字符串
     * /
    公共职能getAuthPassword()
    {
        返回$这个 - >密码;
    }    / **
     *获取其中password提醒发送到的电子邮件地址。
     *
     * @返回字符串
     * /
    公共职能getReminderEmail()
    {
        返回$这个 - >电子邮件;
    }/ **
 *获取用户的唯一标识符。
 *
 * @返回混合
 * /
公共职能getAuthIdentifier()
{
    返回$这个 - > getKey();
}/ **
 *获取用户的密码。
 *
 * @返回字符串
 * /

}

用户播种机:

 < PHP
类UserSeeder扩展播种机{    公共职能的run()
    {
         DB:表(用户) - GT;删除();
        返回数组('表'=>'用户',
        阵列(
                用户名= GT; '管理员',
                '密码'=> 管理员
         )
        );
    }
}


解决方案

当你问的验证类尝试登录,您的用户名传递并通过,因为它是。但如果你看看到方法,它会首先散列密码,以确保安全,然后与数据库条目相匹配。当你保存它,从present实施,它不是散列。

正如上文所述,你应该在你的播种机这种变化:

 阵列(
    用户名= GT; '管理员',
    '密码'=>哈希::让('密码')

虽然我不是很确定,如果你使用的是播种机的方式是通过语法正确的,但如果它的工作原理,只是哈希密码那里。

In Laravel4,I have written the following code in routes but it always redirect me to login page. I have googled and found it on stack overflow too and tried all solutions but not succeeded.I am sure it would be a silly mistake but kindly track it out.Thank You

Routes:

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 */
            }
    });

User Model:

<?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
 */

}

User Seeder:

<?php
class UserSeeder extends Seeder {

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

解决方案

When you ask the Auth class to attempt a login, you pass in the username and pass as it is. But if you look into the method, it will first hash the password to make it secure and then match it with database entry. When you are storing it, from your present implementation, its not hashed.

As suggested above, you should make this change in your seeder:

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

Although I am not very sure if the way you are using seeder is correct by syntax, but if it works, just hash the password there.

这篇关于登录身份验证laravel 4失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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