Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LARAVEL Payload is invalid error but decrypted data is correct

Recently i made new project with composer and added very basic authentication with make:auth - nothing fancy.

Next i wanted to encrypt name and email columns on my database so i changed their types from VARCHAR(191) to LONGTEXT and added some very basic mutators to Users model

public function setNameAttribute($value) {
    $this->attributes['name'] = Crypt::encryptString($value);
}

public function getNameAttribute($value) {
    return Crypt::decryptString($value);
}

public function setEmailAttribute($value) {
    $this->attributes['email'] = Crypt::encryptString($value);
}

public function getEmailAttribute($value) {
    return Crypt::decryptString($value);
}

But when im testing with my very simple route i get Payload is invalid error even though i see in error contents that fields has been decrypted properly.

Route::get('user',function(){
$user= \App\User::find(3);
//dd($user->name);
dd(Crypt::decryptString($user->name));
dd(Crypt::decryptString($user->email));

});

Screenshot link https://ibb.co/deBPpR

like image 792
180doman Avatar asked Jan 31 '18 11:01

180doman


2 Answers

The error is because you are calling Crypt::decryptString on an already mutated (and decrypted) name property. i.e your getNameAttribute decrypts the encrypted string and when you call

Crypt::decryptString($user->name); // this is causing the error

You basically pass a decrypted string for decryption again.

Just do:

echo $user->name;

and you will get the decrypted name.

If you really want to see the raw value then use:

echo $user->getOriginal('name'); //get original value from DB bypassing the accessor
like image 131
Sapnesh Naik Avatar answered Sep 20 '22 12:09

Sapnesh Naik


Payload is invalid is also the response I get when I try to decrypt a null value. Make sure to catch null values before decrypting.

like image 37
JuliaJ Avatar answered Sep 20 '22 12:09

JuliaJ