I am using Laravel 5.4
.
Recently, I have had a requirement to encrypt the data of users table
. So that, I have followed [this][1] link, and created an Encryptable trait
as,
<?php
namespace App\Traits;
use Config;
use Crypt;
trait Encryptable
{
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if (in_array($key, $this->encryptable) && Config::get('app.encrypt_data_at_rest')) {
$value = Crypt::decrypt($value);
}
return $value;
}
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable) && Config::get('app.encrypt_data_at_rest')) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
}
and used it in my User model
like;
<?php
namespace App\Models;
class User extends BaseModel implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract {
use SoftDeletes;
use Encryptable;
protected $encryptable = ['username'];
And inserted a record and the username
encrypted successfully.
But now, the issue is, I am not able to login using this username
and password
.
In the database, the username
seems encrypted. If I get that specific user's username
in my controller, (e.g. User::find(89)->username
) then it returns me the decrypted username (i.e. simple string that I have inserted while creating this user) due to getAttribute()
method of Encryptable trait
.
But however I am not able to use Auth::attempt()
method as it returns false
. I am passing the non-encrypted username
and password
while using Auth::attempt()
method.
Any suggestion how to fix this?