How to handle Composite key with JWT using Laravel 8.0

Viewed 37

I'm using laravel 8.0 with Socialite 5.2.6 and I have composite key in my T_USERS table protected $primaryKey = ["USER_ID","PROVIDER_NAME"];

Now i'm able to create jwt as you can see below in postman.

{
            "EMAIL_ADDRESS":"hello@hello.com",
            "PASSWD_ID":"hello"
}

// got the token back

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcmVzdC5jb21cL2FwaVwvbG9naW4iLCJpYXQiOjE2NDE3MTMyNDksImV4cCI6MTY0MTcxNjg0OSwibmJmIjoxNjQxNzEzMjQ5LCJqdGkiOiJ0R3RodTliTHB6cUFZT0hZIiwic3ViIjp7IlVTRVJfSUQiOiJoZWxsb0BoZWxsby5jb20iLCJQUk9WSURFUl9OQU1FIjoiQUktUk9CT1RTIn0sInBydiI6ImMyZmNjMWJmMWZkMDM3OGM5MGZmMzkxMmVlYzNkOTdhNTg0ZTIwMzUifQ.N7P2jd1S9Z4bLYh2ijVvuDLVzMuvd-wFnSl_2uRuHaU",
    "token_type": "bearer",
    "expires_in": 3600
}

When i use this jwt key in header for authenticated route like me endpoint of jwt URL https://rest.com/api/me by setting Content Type and Accept as application/json. It throws column not found error as shown below.

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from T_USERS where (0 = USER_ID and 1 = PROVIDER_NAME) limit 1)"

How ever Laravel doesn't support composite key and we override function like protected function setKeysForSaveQuery($query) and use query builder instead eloquent. But I don't know how jwt is treating my composite keys.

This is my T_USERS migration file.

 public function up()
    {
        Schema::create('T_USERS', function (Blueprint $table) {
            $table->string('USER_ID')->length(50);
            $table->enum('PROVIDER_NAME',['COMPANY','GOOGLE']);
            $table->string('PROVIDER_ID')->length(255)->nullable();
            $table->string('STATUS_ID')->length(4);
            $table->string('USER_NAME')->length(300);
            $table->string('EMAIL_ADDRESS')->length(300);
            $table->enum('USER_CATEGORY',['USER','CUSTOMER']);
            $table->enum('USER_TYPE',['DIRECT','ORGINTERNAL']);
            $table->string('PASSWD_KEY')->length(250)->nullable();
            $table->string('ROLE_ID')->length(4);
            $table->primary(['USER_ID','PROVIDER_NAME'],'USER_ID');
            $table->foreign('STATUS_ID')->references('STATUS_ID')->on('T_STATUS')->onDelete('cascade')->onUpdate('cascade');
            $table->string('CREATED_DATE')->useCurrent()->nullable();
            $table->string('CREATED_BY')->length(10)->nullable();
            $table->string('MODIFIED_DATE')->useCurrent()->nullable();
            $table->string('MODIFIED_BY')->length(10)->nullable();
            $table->tinyInteger('IS_VERIFIED')->length(1)->default('0');
            $table->tinyInteger('IS_DELETED')->length(1)->default('0');
            $table->timestamps();
        });
    }

Is there any function i have to override or anything to take care in Laravel while using jwt or composite key please let me know.

Thank you in Advance.

0 Answers
Related