[英]Laravel authentication fails
我剛開始使用Laravel,但是我的身份驗證仍然有問題。 我需要在現有數據庫結構上構建系統。 用戶表密碼使用md5,我想將其轉換為Laravel哈希。 但是在將md5密碼轉換為哈希后,登錄失敗並顯示新哈希。 我找不到此問題的解決方案。
表
用戶模型
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'gebruikers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('wachtwoord');
}
登錄控制器
class LoginController extends BaseController {
function starten(){
$rules = array(
'gebruikersnaam' => 'required', // make sure the email is an actual email
'wachtwoord' => 'required' // password can only be alphanumeric and has to be greater than 3 characters
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$user = User::where('gebruikersnaam','=',Input::get('gebruikersnaam'))
->first();
if(isset($user)) {
if($user->wachtwoord == md5(Input::get('wachtwoord'))) {
$user->wachtwoord = Hash::make(Input::get('wachtwoord'));
$user->save();
}
}
$userdata = array(
'gebruikersnaam' => Input::get('gebruikersnaam'),
'wachtwoord' => Input::get('wachtwoord')
);
if (Auth::attempt($userdata)) {
return Redirect::to('users/dashboard')
->with('message', 'You are now logged in!');
} else {
return Redirect::to('/')
->with('message', 'Deze combinatie van gebruikersnaam en wachtwoord lijkt helaas niet te kloppen')
->withInput();
}
}
}
}
驗證碼
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'gebruikers',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
'username' => 'gebruikersnaam',
'password' => 'wachtwoord',
);
注意:Input :: get('wachtwoord')和Input :: get('gebruikersnaam')在控制器中正確地用post填充。 md5在我的數據庫中被正確地更改為Laravel哈希,所以我找不到我要處理的問題。
注意2:“ gebruikersnaam”是荷蘭語的用戶名,“ wachtwoord”是荷蘭語的密碼
注意3:我使用Laravel 4
**編輯**
輸出$ user
[“屬性”:受保護] =>數組(16){[“ id”] => int(8681)[“ gebruikersnaam”] =>字符串(22)“ ---” [“ wachtwoord”] =>字符串( 60)“ $ 2y $ 10 $ i3bemDK9NzNf / E0jmliv / eBPrqhq / 3s3WGPTX3h6WNCMlXcS5W51i” [“ email”] =>字符串(22)“ ---” [“ voornaam”] =>字符串(5)“ Jelle” [“ tussenvoegsel” => NULL [“ achternaam”] =>字符串(4)“ Pals” [“ school_id”] => int(110)[“ telefoonnummer”] =>字符串(10)“ 0655684308” [“ geslacht”] =>字符串(3)“ man” [“ geboortedatum”] =>字符串(19)“ 1990-03-22 09:00:00” [“ groep_id”] => int(8811)[“ status”] =>字符串(1 )“ 1” [“ updated_at”] =>字符串(19)“ 2014-06-25 14:53:43” [“ created_at”] =>字符串(19)“ 0000-00-00 00:00:00” [“ remember_token”] =>字符串(0)“”}
**在updated_at中發現了奇怪的000000 **
我認為問題在於您使用的是默認的Eloquent用戶設置(UserTrait),該設置假定您的密碼字段為“密碼”(請參閱此處 ),但是您使用的是“ wachtwoord”。 這樣,由於在password列中缺少值(由於完全沒有該列)來測試密碼,登錄失敗。
如果您正在考慮“但我明確在我的身份驗證配置中指定了密碼列!” 不幸的是,這是一個紅色的鯡魚-配置的一部分是用於如果您使用標准DB身份驗證,而您正在使用Eloquent身份驗證(請注意,在您的配置中,如何將driver
設置為eloquent
?),所以您需要根據接口在模型中指定這些字段。
幸運的是,類自己的實現會覆蓋特征的實現,因此從理論上講,您可以將特征保留在模型中,而僅覆蓋getAuthPassword
方法。
就是說,該特征(和值得提醒的一個特征)確實非常假設您在各處都使用英語名稱,因此可能有必要完全刪除這兩個特征( UserTrait
和RemindableTrait
)並編寫自己的接口方法版本,如下所示如果/當您使用“記住我”或“忘記密碼”時,您可能會看到同樣的事情。
編輯:初步建議:
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'gebruikers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('wachtwoord');
/**
* Override UserTrait#getAuthPassword
*/
public function getAuthPassword()
{
return $this->wachtwoord;
}
}
但是,正如我說的那樣,如果繼續使用荷蘭語列名,那么最好還是完全放棄特征並自己實現這些方法:
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 = 'gebruikers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('wachtwoord');
/**
* 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->wachtwoord;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->aandenken_onthouden; // okay I made this up based on Google translate and some guesswork, use your own version!
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->aandenken_onthouden = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'aandenken_onthouden';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.