Closed
Description
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 customTokenRepository
andAccessTokenRepository
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 commentedon Aug 5, 2019
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 commentedon Aug 29, 2019
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?
Refactor Token with Contract to allow custom Token models
driesvints commentedon Sep 2, 2019
Since Taylor rejected the PR I don't think we'll provide support for this soon, sorry.
uriel2707 commentedon Nov 7, 2024
@alasdairmackenzie @jmosul https://packagist.org/packages/webdecero/webdecero-package-laravel-passport