Skip to content

Can't use custom Token class that doesn't extend Laravel\Passport\Token #968

@alasdairmackenzie

Description

@alasdairmackenzie

My code is attempting to call $user->createToken() via the Laravel\Passport\HasApiTokens\HasApiTokens trait whilst using a custom Token class that extends a non Laravel model class. However, because the save() function inside the TokenRepository expects it to be a Laravel\Passport\Token, this is not possible.

    L77: public function save(Token $token)

Instead I get an exception

"Argument 1 passed to Laravel\Passport\TokenRepository::save() must be an instance of Laravel\Passport\Token, instance of XXX\Mongodb\Passport\Token given, called in XXX/vendor/laravel/passport/src/PersonalAccessTokenFactory.php on line 77

I can see two options:

  • Option 1: create an interface/contract for Token and use that instead then it would be possible to resolve a custom TokenRepository and AccessTokenRepository that uses that interface on the save function.
  • Option 2: remove the type hint in the save() parameter

While I prefer option 1, I don't see any interfaces being used internally by the package and there are a lot of parameters that aren't type-hinted. This leaves me leaning towards option 2.

I'm happy to create a PR but unsure which direction to take or if a change would be accepted for this.

Can you provide some input on the direction I should take? Thanks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions