2014-03-01 117 views
0

我在做什麼錯?身份驗證可以正常工作,但不會存儲會話(但會話正常工作),無論數據庫的連接是否正確,對我而言,所有內容看起來都正確,但鏈接無效。Laravel身份驗證失敗

Route::get('login', function(){ 
    return View::make('user.login'); 
}); 


Route::post('login', function(){ 

    if (Auth::attempt(array('user' => Input::get('user'), 'password' => Input::get('password')), true)){ 
     return Redirect::to('home'); 
    }else{ 
     return Redirect::to('login')->with('error', 'blah blah'); 
    } 

}); 




Route::group(array('before' => 'auth'), function() 
{ 

    Route::get('home', function(){ 
     echo 'welcome ' . '<br />' . PHP_EOL; 

     echo 'welcome '. Auth::user()->name . ', your Id is: '.Auth::user()->id ; 
    }); 
}); 

** * ** -------------------------- ** * ** * *

<?php 
//testing session if it's working 
if (Session::has ('asdf')) { 
    Session::put('asdf', Session::get('asdf') + 1); 
} else { 
    Session::put('asdf', 1); 
} 

echo Session::get ('asdf'); 

//testing authentication 
if (Auth::check()) 
{ 
    echo 'The user is logged in...'; 
} 

?> 

@if (Session::has('error')) 
<span>{{ Session::get('error') }}</span> 
@endif 


{{ Form::open(array('url' => 'login')) }} 

<table> 
    <tr> 
    <td>{{ Form::label('user', 'User'); }}</td> 
    <td>{{ Form::text('user'); }}</td> 
    </tr> 
    <tr> 
    <td>{{ Form::label('password', 'Password'); }}</td> 
    <td>{{ Form::password('password'); }}</td> 
    </tr> 
    <tr> 
    <td colspan="2">{{ Form::submit('Go!'); }}</td> 
    </tr> 
</table> 

{{ Form::close() }} 

** * ** * - 模型 - * ** * ****

<?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 = 'user'; 

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

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

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

} 

** * ** ** * **

CREATE TABLE IF NOT EXISTS `user` (
    `id_user` tinyint(3) unsigned NOT NULL AUTO_INCREMENT, 
    `created_at` timestamp NOT NULL, 
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `user` varchar(255) NOT NULL, 
    `password` varchar(255) NOT NULL, 
    `name` varchar(30) NOT NULL, 
    `last_name` varchar(30) NOT NULL, 
    `unread_messages` tinyint(4) NOT NULL, 
    `address` varchar(70) NOT NULL, 
    `city` varchar(40) NOT NULL, 
    `state` char(2) NOT NULL, 
    `zip_code` char(5) NOT NULL, 
    `social_security` varchar(255) NOT NULL, 
    `birth_date` date NOT NULL, 
    `salary` float(4,2) NOT NULL, 
    `card_number` varchar(16) NOT NULL, 
    `lang` enum('en','es') NOT NULL DEFAULT 'en', 
    `user_type` enum('admin','user') NOT NULL, 
    `status` enum('ON','OFF') NOT NULL, 
    `id_branch` tinyint(4) NOT NULL, 
    PRIMARY KEY (`id_user`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; 

回答

0

它是否將您重定向到主頁?

echo 'welcome '. Auth::user()->name . ', your Id is: '.Auth::user()->id ; 

這個輸出是什麼?


答:

protected $primaryKey = "id_user"; 

添加到用戶模式

+0

它重定向到我的主頁,但主頁時,我所說的過濾器「身份驗證」,它返回失敗(當登錄已成功),因此重定向到我的登錄頁面。 該代碼用於顯示存儲在數據庫中的用戶名和其Id(http://laravel.com/docs/security#authenticating-users) – user3369524

+0

您可以發佈您的用戶模型嗎?這可能是問題所在。代碼本身似乎沒問題。 – Wallaaa

+0

我用de用戶模型更新了帖子,加上數據庫中的表 – user3369524