mardi 29 janvier 2019

How do you add extra fields to the database when a user registers in Laravel?

I am trying to add a default value into my users table when a user registers. I have a trait which I use for my encryption and decryption with LibSodium.

This is the method that I am calling in my register create method to assign the database with values upon user registration.

trait Cipherous
{
    protected function issueKeys()
    {
        $pki = sodium_crypto_box_keypair();
        return (object) ['private' => sodium_crypto_box_secretkey($pki), 'public' => sodium_crypto_box_publickey($pki)];
    }
}

This method works fine if I create a new controller, attach a web route and manually visit it. However, inside of my \App\Http\Controllers\Auth\RegisterController.php file, in the create method, when I try to use this:

use Cipherous;

protected function create(array $data)
{
    $pki = $this->issueKeys();

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'public_key' => base64_encode($pki->public),
        'private_key' => base64_encode($pki->private),
    ]);
}

It executes fine when I register a new account but when I look in the database it is empty. The expected data being stored looks like this after I use dd($this->issueKeys()) in my test controller.

{#631 ▼
  +"private": b"ÐOÝ‗QÉð\x18áÿ^¶ÄuıÁ£└û\x04Úê▀ÀäÆ=ÒÍÁHì"
  +"public": b"Ù(PöÝ╠+J¼¦│®╬═*;$¹‗ƒ\x11Y╝│§ïÉïƒfI\e"
}

How can I store these bits of data in the database when the user registers? I am using base64_encode() because it contains illegal character offsets.

In-case you need this, here is my migration which appended these columns to my user table.

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        # Set to nullable because some users are already registered
        $table->string('public_key')->unique()->nullable();
        $table->string('private_key')->unique()->nullable();
    });
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2RWdpNy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire