[英]Use Laravel 4 Auth with custom table name
我正在基於現有數據庫創建一個新的laravel 4后端。 然后,我有一個名為“ FormMembers”的表,其中包含我的成員的電子郵件和密碼。
我想使用Auth ::函數,例如:
$credentials = array(
'sEmail' => Input::get('email'),
'sPassword' => Input::get('password')
);
if (Auth::attempt($credentials)) {
return Response::json(Auth::user());
} else {
return Response::json(array('flash' => 'invalid password or email.'), 500);
}
因此,我將auth.php中的表字段從'users'更改為'FormMembers',並對Users模型進行了相同的操作。 但是當調用Auth :: user()時出現錯誤:
{"error":{"type":"ErrorException","message":"Undefined index: password","file":"\/home\/www\/accred\/web\/vendor\/laravel\/framework\/src\/Illuminate\/Auth\/EloquentUserProvider.php","line":75}}
您知道我該如何管理嗎? 謝謝
雄辯的用戶提供者需要憑據陣列中的password
字段。 您通過鍵sPassword
傳遞了一個數組。
你應該能夠做
$credentials = array(
'password' => Input::get('email'),
'sEmail' => Input::get('password'),
);
Auth::attempt($credentials);
並處理用戶雄辯模型中的其余部分。 即使您的用戶表具有不同的列。
編輯:Auth :: attempt()函數由Illuminate的Guard類處理。 這將調用實現UserProviderInterface
的類的retrieveByCredentials
,然后是validateCredentials
方法。 默認情況下,這是EloquentUserProvider
。
除了您的憑據數組中包含密碼的密鑰外, retrieveByCredentials
方法使用所有其他內容。 它返回一個User雄辯的模型對象。 因此,請在此處使用“ sEmail”鍵。
雄辯的用戶提供者上調用的validateCredentials
方法將檢查憑據數組中password
字段的哈希值是否與從User對象上的getAuthPassword
方法檢索的哈希密碼相匹配(需要實現),因為它實現了UserInterface
。
因此,您應該覆蓋User類的getAuthPassword
方法。
public function getAuthPassword(
{
return $this->sPassword;
}
這樣,它將返回存儲在數據庫中的哈希密碼。
當然,您的密碼應該像Hash :: make一樣進行哈希處理。 如果不:
BcryptHasher
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.