Reputation: 11
I just cant seem to find a solution, my Auth::attempt()
function always return false and I cant find what I am doing wrong. This is my migration for tblusers:
public function up()
{
Schema::create('tblusers', function (Blueprint $table) {
$table->id();
$table->string('Username')->unique();
$table->string('FirstName');
$table->string('LastName');
$table->string('ContactNo')->unique();
$table->string('Email')->unique();
$table->timestamp('EmailVerifiedOn')->nullable();
$table->string('Password');
$table->integer('AddedBy');
$table->rememberToken();
$table->timestamps();
});
}
I customized the User
model:
class User extends Authenticatable
{
protected $table = 'tblusers';
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'Password', 'remember_token',
];
}
Therefore the password is no longer visible on User::
queries, so I use Auth::attempt
instead. This is how I insert my hashed password:
$password = $request->Password;
$hash = Hash::make($password);
$insertData = array(
...
'Email' => $email,
'Password' => $hash,
'AddedBy' => 0,
'created_at' => date('Y-m-d H:i:s')
);
This is how I use Auth::attempt
, can somebody point out what I'm doing wrong as it always returns false.
$credentials = array(
"Username" => $username,
"Password" => Hash::make($password)
);
return dd(Auth::attempt($credentials));
if (Auth::attempt($credentials)) {
$response = true;
} else {
$response = false;
}
Upvotes: 1
Views: 130
Reputation: 50481
The 'password' field that is passed to attempt
MUST be named password
(exactly like this). Also you will need to adjust the getAuthPassword
method on your Model since by default it expects the password field in the database to be 'password'. If this is a new project and database I highly suggest you follow the conventions, but if you want to keep things the way they are, these are the changes you will need to make:
$credentials = [
'Username' => $username,
'password' => $password,
];
Notice the key is password
and we are not hashing the password, as it is expecting the plain-text version of the password so it can eventually do a hash check on it.
On this Model you will need to override the getAuthPassword
method:
public function getAuthPassword()
{
return $this->Password;
}
Upvotes: 1