user model with PK type string

I encountered with a rather strange bug: - I installed a fresh laravel 11 app, then I added the filement plugin, migrated the database, created a user, and I was able to log in to the panel without any issue. Because of business requirements, I want to change the User model primary key to uuid ($table->uuid('id')->primary(); in my migration file), I also added public $keyType = 'string'; public $incrementing = false; ... public static function boot() { parent::boot(); static::creating(function ($model) { $model->id = (string) uniqid("usr_"); }); } to my User model. Refreshed the database, recreated a new user, but after these changes I cannot log in anymore into the app. I debugged the app, the user record queried correctly at login, but it redirects back to the login screen, because in the vendor/filament/filament/src/Http/Middleware/Authenticate.php file, line 22: if (!$guard->check()) { $this->unauthenticated($request, $guards); return; } the $guard->check() returns null. Could you give me any clue, why it isn't working with PK type string?
10 Replies
awcodes
awcodes7mo ago
You shouldn’t need any of that on the user model. Just add the Illuminate\Database\Eloquent\Concerns\HasUuids trait to your user model.
Roland Barkóczi
Roland BarkócziOP7mo ago
Unfortunately still not work. If the login_web_xxxxx key has any non-numerical value in the session , something delete it. It seems it is not the Illuminate/Auth/AuthManager or the SessionGuard It is independent form the type of User id: if I just rewrite the id form usr_382523 to 1, it works as expected
awcodes
awcodes7mo ago
Ah, that makes sense. ‘usr_’ isn’t a valid uuid.
Roland Barkóczi
Roland BarkócziOP7mo ago
i see
awcodes
awcodes7mo ago
UUID is a specific formatted string
Roland Barkóczi
Roland BarkócziOP7mo ago
maybe i will re-think my model, and stick to numerical PK, and I add a secondary id to prevent scrapping public website in any case, thank you so much for your help!
awcodes
awcodes7mo ago
Any particular reason you need the prefix? You know what the model is so the prefix shouldn’t be necessary.
Roland Barkóczi
Roland BarkócziOP7mo ago
I got the idea from stripe where all the different items (customer, invoice, etc) has a prefix in order to easily identify the item type just by the id
awcodes
awcodes7mo ago
Ok. Fair enough.
Roland Barkóczi
Roland BarkócziOP7mo ago
Thank you very much for your help!
Want results from more Discord servers?
Add your server