Laravel Apiato Using uuid in users table

Laravel Apiato Using uuid in users table

Modify User Model

first things you must to do is use libray or something to provide uuid, you can use ramsey/uuid or use from laravel facades Illuminate\Database\Eloquent\Concerns\HasUuids.

namespace App\Containers\AppSection\User\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;

class User extends ParentUserModel implements MustVerifyEmail
{
 use HasUuids;
...

public $incrementing = false;
...

protected $casts = [
        ...
        'id' => 'string'
    ];
}

Modify Migration Table

Next step is modify your migration file to make sure all field from bigInt to char(36), use this reference for spatie migration table

User migration table

look at your file _create_users_table in ...\Containers\AppSection\User\Data\Migrations\2000_01_01_000001_create_users_table.php and change $table->id(); to $table->uuid('id');

Modify model_has_roles table

in file ...\Containers\AppSection\Authorization\Data\Migrations\2016_12_29_201047_create_permission_tables.php find table model_has_roles and model_has_permissions modify like this

$table->unsignedBigInteger($columnNames['model_morph_key']);

// change to

$table->uuid($columnNames['model_morph_key']);

modify oauth_access_tokens table

go to ...\app\database\migrations\2016_06_01_000002_create_oauth_access_tokens_table.php change $table->unsignedBigInteger('user_id')->nullable()->index(); to $table->uuid('user_id')->nullable()->index();

modify oauth_auth_codes table

go to ...\app\database\migrations\2016_06_01_000001_create_oauth_auth_codes_table then change $table->unsignedBigInteger('user_id')->index(); to $table->uuid('user_id')->index();

Modify email verification notification

default notification is use hashed id which is auto increment id, but if we use uuid we cannot use that, so we must modify verify email file go to ...\app\Containers\AppSection\Authentication\Notifications\VerifyEmail.php

private function createUrl(UserModel $notifiable): string
{
  $id = config('apiato.hash-id') ? $notifiable->getHashedKey() : $notifiable->getKey(); // this code will be modify
  // change to this
  $id = $notifiable->getKey();

        $hash = sha1($notifiable->getEmailForVerification());

        return $this->verification_url . '?url=' . URL::temporarySignedRoute(
                'verification.verify',
                now()->addMinutes(config('appSection-authentication.verification_link_expiration_time')),
                [
                    'id' => $id,
                    'hash' => $hash,
                ]
            );
}

from code above link verification will use uuid key, alternatively you can set in .env change HASH_ID=true to HASH_ID=false. After you modify verify email notification next step is modify verification.verify this will receive token and id to validated your email verification, go to ...\app\Containers\AppSection\Authentication\UI\API\Requests\VerifyEmailRequest.php this request file is use in ...\app\Containers\AppSection\Authentication\UI\API\Controllers\VerifyEmailController.php then comment id from decode, this will make request receive real id which is uuid than hash id.

 protected array $decode = [
        // 'id',
    ];

Run migration and seed data

last step is do that migration php artisan migrate --seed

Did you find this article valuable?

Support Belajar Backend by becoming a sponsor. Any amount is appreciated!