I recently started learning Laravel (PHP MVC framework) and I've been having a lot of issues with Authenticating a user with: Auth::attempt($credentials).
I'll put my code snippets below. In my mySQL database I have a record with the following key=>values:
id=>1, username=>'admin', password=>'admin', created_at=>0000-00-00 00:00:00, updated_at=>0000-00-00 00:00:00
The Validator passes, but the Auth:attempt always fails.
If the code below and my little explanation isn't enough, please let me know! :-)
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'));
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…