Relevant composer.json:
7 "require": {
8 "php": ">=5.5.9",
9 "laravel/framework": "5.2.*",
10 "tymon/jwt-auth": "^0.5.9"
11 },
Here is the code (line numbers are only for reference - I have them set on in my editor settings):
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use JWTAuth;
6 use Tymon\JWTAuth\Exceptions\JWTException;
7
8 use Illuminate\Http\Request;
9
10 use App\Http\Requests;
11
12 class Authentication extends Controller {
13 public function index() {
14 return view('/home');
15 }
16
17 public function authenticate(Request $request) {
18 $credentials = $request->only('email', 'password');
19
20 try {
21 // attempt to verify the credentials and create a token for the user
22 if (! $token = JWTAuth::attempt($credentials)) {
23 return response()->json(['error' => 'invalid_credentials'], 401);
24 }
25 } catch (JWTException $e) {
26 // something went wrong whilst attempting to encode the token
27 return response()->json(['error' => 'could_not_create_token'], 500);
28 }
29
30 // all good so return the token
31 return response()->json(compact('token'));
32 }
33 }
Notice that it's fairly much what is here: https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens
"php -l" doesn't find any errors in it.
However, when I go to that code (via /authenticate setup like this) in Http/routes.php:
33 Route::group(['middleware' => 'web'], function () {
34 Route::auth();
35
36 Route::get('/home', ['middleware' => 'auth', 'uses' => 'HomeController@index']);
37
38 Route::any('/authenticate', ['uses' => 'Authentication@authenticate']);
39 });
(Note: this is a default from Laravel 5.2 - only line 38 is added).
What I get is:
Whoops, looks like something went wrong.
1/1
FatalErrorException in Authentication.php line 22:
Class 'JWTAuth' not found
1. in Authentication.php line 22
Autload has this:
[lloy0076@localhost composer]$ vim autoload_real.php
[lloy0076@localhost composer]$ grep -r 'Tymon' *
autoload_classmap.php: 'Tymon\\JWTAuth\\Blacklist' => $vendorDir . '/tymon/jwt-auth/src/Blacklist.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Audience' => $vendorDir . '/tymon/jwt-auth/src/Claims/Audience.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Claim' => $vendorDir . '/tymon/jwt-auth/src/Claims/Claim.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\ClaimInterface' => $vendorDir . '/tymon/jwt-auth/src/Claims/ClaimInterface.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Custom' => $vendorDir . '/tymon/jwt-auth/src/Claims/Custom.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Expiration' => $vendorDir . '/tymon/jwt-auth/src/Claims/Expiration.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Factory' => $vendorDir . '/tymon/jwt-auth/src/Claims/Factory.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\IssuedAt' => $vendorDir . '/tymon/jwt-auth/src/Claims/IssuedAt.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Issuer' => $vendorDir . '/tymon/jwt-auth/src/Claims/Issuer.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\JwtId' => $vendorDir . '/tymon/jwt-auth/src/Claims/JwtId.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\NotBefore' => $vendorDir . '/tymon/jwt-auth/src/Claims/NotBefore.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Claims\\Subject' => $vendorDir . '/tymon/jwt-auth/src/Claims/Subject.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Commands\\JWTGenerateCommand' => $vendorDir . '/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\InvalidClaimException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/InvalidClaimException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\JWTException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/JWTException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\PayloadException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/PayloadException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\TokenBlacklistedException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/TokenBlacklistedException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\TokenExpiredException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/TokenExpiredException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Exceptions\\TokenInvalidException' => $vendorDir . '/tymon/jwt-auth/src/Exceptions/TokenInvalidException.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Facades\\JWTAuth' => $vendorDir . '/tymon/jwt-auth/src/Facades/JWTAuth.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Facades\\JWTFactory' => $vendorDir . '/tymon/jwt-auth/src/Facades/JWTFactory.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\JWTAuth' => $vendorDir . '/tymon/jwt-auth/src/JWTAuth.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\JWTManager' => $vendorDir . '/tymon/jwt-auth/src/JWTManager.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Middleware\\BaseMiddleware' => $vendorDir . '/tymon/jwt-auth/src/Middleware/BaseMiddleware.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Middleware\\GetUserFromToken' => $vendorDir . '/tymon/jwt-auth/src/Middleware/GetUserFromToken.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Middleware\\RefreshToken' => $vendorDir . '/tymon/jwt-auth/src/Middleware/RefreshToken.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Payload' => $vendorDir . '/tymon/jwt-auth/src/Payload.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\PayloadFactory' => $vendorDir . '/tymon/jwt-auth/src/PayloadFactory.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\Auth\\AuthInterface' => $vendorDir . '/tymon/jwt-auth/src/Providers/Auth/AuthInterface.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\Auth\\IlluminateAuthAdapter' => $vendorDir . '/tymon/jwt-auth/src/Providers/Auth/IlluminateAuthAdapter.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\JWTAuthServiceProvider' => $vendorDir . '/tymon/jwt-auth/src/Providers/JWTAuthServiceProvider.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\JWT\\JWTInterface' => $vendorDir . '/tymon/jwt-auth/src/Providers/JWT/JWTInterface.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\JWT\\JWTProvider' => $vendorDir . '/tymon/jwt-auth/src/Providers/JWT/JWTProvider.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\JWT\\NamshiAdapter' => $vendorDir . '/tymon/jwt-auth/src/Providers/JWT/NamshiAdapter.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\Storage\\IlluminateCacheAdapter' => $vendorDir . '/tymon/jwt-auth/src/Providers/Storage/IlluminateCacheAdapter.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\Storage\\StorageInterface' => $vendorDir . '/tymon/jwt-auth/src/Providers/Storage/StorageInterface.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\User\\EloquentUserAdapter' => $vendorDir . '/tymon/jwt-auth/src/Providers/User/EloquentUserAdapter.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Providers\\User\\UserInterface' => $vendorDir . '/tymon/jwt-auth/src/Providers/User/UserInterface.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Token' => $vendorDir . '/tymon/jwt-auth/src/Token.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Utils' => $vendorDir . '/tymon/jwt-auth/src/Utils.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Validators\\AbstractValidator' => $vendorDir . '/tymon/jwt-auth/src/Validators/AbstractValidator.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Validators\\PayloadValidator' => $vendorDir . '/tymon/jwt-auth/src/Validators/PayloadValidator.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Validators\\TokenValidator' => $vendorDir . '/tymon/jwt-auth/src/Validators/TokenValidator.php',
autoload_classmap.php: 'Tymon\\JWTAuth\\Validators\\ValidatorInterface' => $vendorDir . '/tymon/jwt-auth/src/Validators/ValidatorInterface.php',
autoload_psr4.php: 'Tymon\\JWTAuth\\' => array($vendorDir . '/tymon/jwt-auth/src'),
installed.json: "Tymon\\JWTAuth\\": "src"
installed.json: "name": "Sean Tymon",
I then had the idea to replace line 5 of the code with:
5 use Tymon\JWTAuth as JWTAuth;
But this just complains:
Whoops, looks like something went wrong.
1/1
FatalErrorException in Authentication.php line 22:
Class 'Tymon\JWTAuth' not found
1. in Authentication.php line 22
Am I doing something obviously wrong? I've included my composer.lock (but in a zip as github won't let me post just the .lock).
PS That optimised autoload looks correct to me - the composer is probably doing stuff right as if it weren't the whole Laravel framework would come crashing down with 'class not found errors' before it got here.
GAH! Missed the JWTAuth fa莽ade in app/config.php.
Ignore me :(
use TymonJWTAuthFacadesJWTAuth;
or
use JWTAuth;
This could solve this problem.
Most helpful comment
use TymonJWTAuthFacadesJWTAuth;
or
use JWTAuth;
This could solve this problem.