I recently started learning Laravel (the PHP MVC framework) and I had a lot of problems with user authentication: Auth :: attempt ($ credentials).
Below are my code snippets. In my mySQL database, I have an entry 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: the attempt always fails.
If the code below and my little explanation is not enough, 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')); }
source share