Refactoring Authentication and Product Association Logic in Reimpact Platform
This post summarizes recent updates to the Reimpact platform, focusing on authentication improvements and product association refinements. The changes include migrating from Passport to Sanctum for authentication, streamlining UUID handling, and refactoring job processing.
Authentication Migration
The primary focus was migrating the authentication system from Laravel Passport to Sanctum. This involved replacing Passport's database queries for token revocation with Sanctum's more direct approach. Previously, revoking a user's tokens required querying the oauth_access_tokens and oauth_refresh_tokens tables. The updated approach uses $user->tokens()->delete(), which is more aligned with Sanctum's token management.
For example, the old Passport-based revocation might have looked like this (illustrative):
// Old Passport approach (illustrative)
DB::table('oauth_access_tokens')
->where('user_id', $user->id)
->update(['revoked' => true]);
DB::table('oauth_refresh_tokens')
->where('access_token_id', $accessTokenId)
->update(['revoked' => true]);
Now, the Sanctum-based revocation is simplified to:
// New Sanctum approach
$user->tokens()->delete();
This change also necessitated updating the authentication tests to assert against the personal_access_tokens table used by Sanctum, instead of the oauth_access_tokens table.
UUID Handling
The HasUuids trait was implemented to automatically handle UUID generation for models. This eliminated the need for manual UUID generation using Str::uuid() in several places, such as in the Brand::afterCreate method and associated tests. By leveraging the trait, the codebase is cleaner and less prone to errors related to UUID generation.
For instance, instead of manually assigning a UUID like this (illustrative):
use Illuminate\Support\Str;
class Brand extends Model
{
public static function booted()
{
static::created(function ($brand) {
$brand->uuid = Str::uuid();
$brand->save();
});
}
}
The HasUuids trait automates this process, making the code more concise.
Job Refactoring
The SaveProducts job was refactored to improve efficiency. A previously serialized constructor parameter ($cachedPriority) was moved to a plain class property. Serializing this parameter was unnecessary and introduced overhead. By making it a class property, the job's execution is more streamlined.
Previously the constructor might have looked like this (illustrative):
class SaveProducts implements ShouldQueue
{
private $cachedPriority;
public function __construct(Priority $cachedPriority)
{
$this->cachedPriority = $cachedPriority;
}
}
Now it looks like this:
class SaveProducts implements ShouldQueue
{
private $cachedPriority;
public function handle(Priority $cachedPriority)
{
$this->cachedPriority = $cachedPriority;
}
}
Code Style and Conventions
Finally, the update addressed inline Fully Qualified Class Name (FQCN) usage by replacing instances of \Illuminate\Support\Str::uuid() and \Features\Enums\FeatureEnum with proper use imports, adhering to project coding standards.
Conclusion
These changes enhance the Reimpact platform by improving authentication security and efficiency, streamlining UUID management, and optimizing job processing. The updates also contribute to a cleaner, more maintainable codebase by adhering to coding standards and removing redundant code.