Reputation: 231
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'));
}
Upvotes: 8
Views: 14016
Reputation: 1238
I believe the attempt() method will hash the password that is sent in. Seeing as your DB entry's password is 'admin' in plaintext, that would fail. Save your password with Hash::make($password); and i believe your problem should be solved.
Upvotes: 23