0

I want to encrypt email address in user table in order to protect personal information. I tried this way

app Encryptable.php

<?php
namespace App;
use Crypt;
trait Encryptable{
    public function getAttribute($key){
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable)) {$value = Crypt::decrypt($value);return $value;}
        return $value;
    }
    public function setAttribute($key, $value){
        if (in_array($key, $this->encryptable)) {$value = Crypt::encrypt($value);}
        return parent::setAttribute($key, $value);
    }
}

app\User.php

<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Encryptable;
class User extends Authenticatable implements MustVerifyEmailContract{
    use MustVerifyEmail, Notifiable;
    use Encryptable;
    protected $fillable = ['name', 'email', 'password',];
    protected $hidden = ['password', 'remember_token',];
    protected $casts = [email_verified_at' => 'datetime',];
    public $encryptable = [email',];
}

app/encryptable.php

<?php
namespace App;
use crypt;
trait Encryptable {
    public function getAttribute($key){
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable)) {$value = Crypt::decrypt($value);return $value;}
        return $value;
    }
    public function setAttribute($key, $value){
        if (in_array($key, $this->encryptable)) {$value = Crypt::encrypt($value);}
        return parent::setAttribute($key, $value);
    }
}

I can encrypt email address. But I can't login and reset password. User can make many accounts in same email address. It is very bad bug.

Help ME!!

different accounts send same question because I can't login. I tried to reset password but It failed. Im sorry.

Dallas Caley
  • 5,367
  • 6
  • 40
  • 69
ru501
  • 11
  • 2
  • I tryied [this way](https://stackoverflow.com/questions/59547255/in-laravel-how-to-encrypt-email-adress-in-user-table).But error Class App\Http\Controllers\Auth\Request does not exist – ru501 Jan 01 '20 at 03:43
  • you don't have to encrypt the email address. usually no one encrypt the email address.and when you are encrypting the email address it's giving different hash values every time that's why you can create multiple account with same email address. – Ruhith Udakara Jan 01 '20 at 04:14
  • Well, I think it is a bad idea to encrypt the email address. Encrypting/hashing the password is enough. And as Ruhith Udakara said, it will created different value. If you want to avoid the multiple using of email, you must decrypt your encrpyted email first. Complicated and unnecessary. – ggsuha Jan 02 '20 at 07:59

0 Answers0