[英]Laravel Auth::attempt() always false?
我最近開始學習Laravel(PHP MVC框架),我在使用以下方式驗證用戶時遇到了很多問題:Auth :: attempt($ credentials)。
我將把我的代碼片段放在下面。 在我的mySQL數據庫中,我有一個記錄,其中包含以下key => values:id => 1,username =>'admin',password =>'admin',created_at => 0000-00-00 00:00:00,updated_at => 0000-00-00 00:00:00
驗證器通過,但Auth:嘗試總是失敗。
如果下面的代碼和我的小解釋還不夠,請告訴我! :-)
routes.php文件:
Route::get('login', 'AuthController@showLogin');
Route::post('login', 'AuthController@postLogin');
AuthController:
public function showLogin(){
if(Auth::check()){
//Redirect to their home page
}
//Not logged in
return View::make('auth/login');
}
public function postLogin(){
$userData = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
$reqs = array(
'username' => 'Required',
'password' => 'Required'
);
$validator = Validator::make($userData, $reqs);
if($validator->passes() && Auth::attempt($userData)){
return Redirect::to('home');
}
return Redirect::to('login')->withInput(Input::except('password'));
}
我相信attempt()方法會對發送的密碼進行哈希處理。看到你的數據庫條目的密碼是明文中的'admin',那就會失敗。 使用Hash :: make($ password)保存密碼; 而且我相信你的問題應該得到解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.