Hi,
After installing the 0.5.0 version of jwt-auth in my Laravel 5 project.
I get the error
FatalErrorException in AuthController.php line 18:
Class 'App\Http\Controllers\Api\JWTAuth' not found
when I access my route.
Here is my code in the controller:
<?php namespace App\Http\Controllers\Api;
use App\Http\Requests;
use App\Http\Controllers\Api\ApiController;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Http\Request;
class AuthController extends ApiController {
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
}
I did follow the wiki, and added the provider and alias
Looks like you need to import the class because you are within a namespace.
Either by prefixing JWTAuth (\JWTAuth) with a backslash where you using it or adding the use statement at the top of your class:
use Tymon\JWTAuth\Facades\JWTAuth;
// or
use JWTAuth;
or you could always inject the instance via the constructor:
<?php namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\ApiController;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Http\Request;
class AuthController extends ApiController {
protected $auth;
public function __construct(JWTAuth $auth)
{
$this->auth = $auth;
}
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = $this->auth->attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
}
That worked, nice!
I'd suggest adding this to the documentation, so other wont encounter the same problem ;-)
Hi,
Please follow the instruction at https://github.com/tymondesigns/jwt-auth/wiki/Installation
it worked for me after changing config/app.php aliases from
'JWTAuth' => Tymon\JWTAuthFacades\JWTAuth::class,
to
'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth',
Change controller line
use JWTAuth;
with
use Tymon\JWTAuth\Facades\JWTAuth;
@vikramparihar Hi, can you help me understand why use JWTAuth throws an error when I try to use toUser() even though I configured it in the config/app.php aliases.
Also, why does use Tymon\JWTAuth\Facades\JWTAuth work, what's the difference?
This fix does not work for Lumen 5.5 ... I receive the following error
BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [$app ]] in class Illuminate\Cache\CacheManager
Please advice ...
Most helpful comment
Looks like you need to import the class because you are within a namespace.
Either by prefixing JWTAuth (
\JWTAuth) with a backslash where you using it or adding the use statement at the top of your class: