xDr*_*nik 5 php authentication laravel
我最近开始学习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');
Run Code Online (Sandbox Code Playgroud)
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'));
}
Run Code Online (Sandbox Code Playgroud)
mad*_*ero 18
我相信attempt()方法将对发送的密码进行哈希处理.看到你的数据库条目的密码是明文中的'admin',那就会失败.使用Hash :: make($ password)保存密码; 而且我相信你的问题应该得到解决.