2013-08-24 84 views
2

在Laravel4中,我在路由中编写了以下代码,但它总是将我重定向到登录页面。 我用Google搜索,发现它的堆栈溢出过,并试图所有的解决方案,但不succeeded.I我相信这将是一个愚蠢的错误,但好心跟踪它out.Thank您登录认证在laravel 4中失败

路线:

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

回答

0

当您要求Auth类尝试登录时,您传入用户名并按原样传递。但是如果你看看这个方法,它会首先对密码进行散列以保证安全,然后将其与数据库条目进行匹配。当你存储它时,从你目前的实现中,它不会被散列。

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

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

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

+0

我已经散列它仍然不能正常工作 auth中的问题:尝试 – Navya

0

你应该散列你的密码。

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

可以在the docs找到更多信息。

+0

我已经散列它仍然没有工作 问题在身份验证:尝试 – Navya

+0

谢谢大家 但我终于得到了错误 不使用哈希::使身份验证密码::尝试 (因为一旦我在播种机已哈希) – Navya

+0

@Navya,你是怎么解决你的问题的?我处于类似的情况。 –