Laravel 4 Auth: attempt does not work

I find it difficult to work with the Laravel 4 Auth :: try method, follow the correct documentation, read a couple of SO threads, but still I can not get it to work.

$userData = array('email' => ' admin@admin.com ','password' => 'admin'); if(Auth::attempt($userData)){ // redirect } else{ echo 'Invalid'; } 

And he returns Invalid everytime

Now I'm not sure what the reason is.

In my config / auth.php I have the following

 <?php return array( /* |-------------------------------------------------------------------------- | Default Authentication Driver |-------------------------------------------------------------------------- | | This option controls the authentication driver that will be utilized. | This drivers manages the retrieval and authentication of the users | attempting to get access to protected areas of your application. | | Supported: "database", "eloquent" | */ 'driver' => 'eloquent', /* |-------------------------------------------------------------------------- | Authentication Model |-------------------------------------------------------------------------- | | When using the "Eloquent" authentication driver, we need to know which | Eloquent model should be used to retrieve your users. Of course, it | is often just the "User" model but you may use whatever you like. | */ 'model' => 'User', /* |-------------------------------------------------------------------------- | Authentication Table |-------------------------------------------------------------------------- | | When using the "Database" authentication driver, we need to know which | table should be used to retrieve your users. We have chosen a basic | default value but you may easily change it to any table you like. | */ 'table' => 'users', /* |-------------------------------------------------------------------------- | Password Reminder Settings |-------------------------------------------------------------------------- | | Here you may set the settings for password reminders, including a view | that should be used as your password reminder e-mail. You will also | be able to set the name of the table that holds the reset tokens. | */ 'reminder' => array( 'email' => 'emails.auth.reminder', 'table' => 'password_reminders', ), ); ?> 
+7
source share
8 answers

Make sure the password field in your db has a space for 64 characters . varchar(64)

A hash requires 64 characters and you won’t get an error from laravel if your hashed password is truncated upon insertion (and therefore cannot ever verify the password positively).

+15
source

@ eric-evans is true. To use authentication in laravel 4, you must force your user model to use the \ Illuminate \ Auth \ UserInterface interface and implement methods

 public function getAuthIdentifier() { return $this->getKey(); } public function getAuthPassword() { return $this->password; } 
+10
source

Could you show the code for your model? These are some of the things that must be in the User model for Auth to work.

 <?php use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface{ protected $fillable = array('fname','lname','email','password','create_at','updated_at'); /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes excluded from the model JSON form. * * @var array */ protected $hidden = array('password'); /** * Get the unique identifier for the user. * * @return mixed */ public function getAuthIdentifier() { return $this->getKey(); } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->password; } /** * Get the e-mail address where password reminders are sent. * * @return string */ public function getReminderEmail() { return $this->email; } } 
+8
source

I had problems with this. I noticed that in my User model I had:

 protected $softDelete = true; 

I had a deleted_at column in the database, but the default timestamp was a timestamp of 0:00 0000-00-00. This led to the fact that the record was not found and, in turn, caused an authentication error.

I had to fix the migration so that the deleted_at column was correctly created as follows:

 public function up() { Schema::create('users', function($t) { $t->increments('id'); $t->string('first_name'); $t->string('last_name'); $t->string('username'); $t->string('email'); $t->string('password'); $t->softDeletes(); $t->timestamps(); }); } 

Here are the soft delete docs: http://laravel.com/docs/eloquent#soft-deleting

+2
source

The Auth class requires a column named id as the primary key in your user table.

0
source

Note that the user table, the Hashed password field, calls Auth :: try ($ credentials); $ credentials-> password value hashed password

0
source

Check if your password is locked. This should not be normal text.

0
source

Your code is called because you are passing the wrong array keys to Auth::attempt() . This method requires an array with a username, password, and possibly storage. In this light, your code should be:

 Route::post('login', function() { $credentials = [ 'username' => ' admin@email.com ', 'password' => 'superSecretPassword' ]; dd(Auth::attempt($credentials)); }); 
-3
source

All Articles