Skip to content

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

Closed
@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

Activity

driesvints

driesvints commented on Aug 5, 2019

@driesvints
Member

Heya. For some reason this issue slipped past by me, sorry for that.

Can you please provide a thorough explanation why you need this?

jmosul

jmosul commented on Aug 29, 2019

@jmosul

We need this as we use MongoDB so our Token class extends a different base Model.

This causes an issue when the TokenRespository save function is called. It is type hinted to only accept the Token from laravel-passport. Which we can't override in our own extension of Laravel Passport designmynight/laravel-mongodb-passport

Can I suggest a Token interface as the type hint?

driesvints

driesvints commented on Sep 2, 2019

@driesvints
Member

Since Taylor rejected the PR I don't think we'll provide support for this soon, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @uriel2707@driesvints@alasdairmackenzie@jmosul

        Issue actions

          Can't use custom Token class that doesn't extend Laravel\Passport\Token · Issue #968 · laravel/passport