codecasts / laravel-jwt

Dead simple, plug and play JWT API Authentication for Laravel (5.4+)
MIT License
234 stars 27 forks source link

BindingResolutionException #6

Open podluzhnyi opened 7 years ago

podluzhnyi commented 7 years ago

After installation strictly according to the instructions, I have a exception:

Unresolvable dependency resolving [Parameter #0 [ $app ]] in class Codecasts\Auth\JWT\Auth\Guard in Container.php (line 910)

Laravel 5.4.28

I did:

  1. composer require codecasts/laravel-jwt
  2. added service provider Codecasts\Auth\JWT\ServiceProvider::class
  3. published vendor files
  4. generated and added secret to .env
  5. changed driver in api guards to 'jwt'
pedrogaldino commented 6 years ago

Same here

hernandev commented 6 years ago

@podluzhnyi @pedrogaldino gonna check it right now

hernandev commented 6 years ago

@pedrogaldino @pedrogaldino can you explain better where are you triggering this?

The Guard instance should be registered on the laravel and it's not instantiable with resolve since it needs some parameters.

the other guards, default ones are the same

phillip-elm commented 6 years ago

@podluzhnyi @pedrogaldino @hernandev - did you guys ever find the cause of this? I'm experiencing this exact same issue on Laravel 5.6 / PHP 7.2.

phillip-elm commented 6 years ago

Just came back from running some errands - took a fresh look at this, I think I figure out what @hernandev meant - this error went away in my code when I stopped trying to have the Guard DI'd into my controller and instead used the Auth facade.

Example:

// This won't even run - the DI will fail. This is adapted from the current README.md.
public function refreshToken(Guard $auth, Request $request)
{
  $token = $auth->refresh();
  return response()->json(compact('token'));
}

Instead, running the following works:

public function refreshToken(Request $request)
{
  $token = \Auth::refresh();
  return response()->json(compact('token'));
}

Once I switched to the latter method, all my tests passed. As such, we probably want to update the README. I'll submit a PR.

Anticom commented 5 years ago

I'm rather new to laravel but this looks kind of fishy to me:

https://github.com/codecasts/laravel-jwt/blob/cd5cf11d4f5be42cbf2d1202612779e84c13a980/src/Auth/ServiceProvider.php#L42-L49

Is there any reason a fresh guard is returned and not $guard ?

jonagoldman commented 5 years ago

@Anticom I have already fixed this in PR https://github.com/codecasts/laravel-jwt/pull/45. @hernandev returning a new instance of the guard resulted in the Auth events not being fired (bug), I fixed that in my PR, but now because Laravel 5.7 changed the events constructor, the package need to be updated for 5.7 also.

This means the next release of the package will be for >= 5.7 only