Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Add Generics to HasApiTokens #544

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Nov 13, 2024

This adds generics to HasApiTokens to aid in static analysis.

This should be beneficial to folks who are overriding the default Sanctum models as outlined in the docs, but also should provide IDE typehint && static analysis benefits.

If you are overriding the personal access token model, then you would add this to your tokenable model.

class User extends Authenticatable
{
    /** @use HasApiTokens<TeamworksAccessToken> */
    use HasApiTokens;

    // ...
}

Otherwise, the default for the generic is already set.

As a bonus, it also adds the generics for tokens() relationship to keep in line with Laravel's MorphMany's generics.

Outstanding Question

Should the same generics be added to the Contracts/HasApiTokens interface? 🤔

@cosmastech cosmastech changed the title Add Generics to HasApiTokens [4.x] Add Generics to HasApiTokens Nov 13, 2024
@taylorotwell taylorotwell merged commit 7856ef8 into laravel:4.x Nov 14, 2024
6 checks passed
@unre4l
Copy link

unre4l commented Nov 25, 2024

Small question, how does this go with https://github.com/laravel/sanctum/blob/4.x/src/Guard.php#L59 ? TransientToken doesnt align with TTokenModel of \Illuminate\Database\Eloquent\Model part.

@cosmastech
Copy link
Contributor Author

Small question, how does this go with https://github.com/laravel/sanctum/blob/4.x/src/Guard.php#L59 ? TransientToken doesnt align with TTokenModel of \Illuminate\Database\Eloquent\Model part.

Ugh. I think you're right about that 😢 . I'll try a PR to remove the Model part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants