Trelaquix
Trelaquix

Reputation: 83

Class 'JWT' not found

I'm trying to run an authentication API using JWT.

I am currently using Slimframework v3 and PHP 7.4.1 and I'm connecting to MySQL using PDO. I used composer to require firebase.

C:\Users\Desktop\php>composer require firebase/php-jwt

I'm getting an error message when running my API using postman saying that "Class 'JWT' not found" line 172.

<h1>Slim Application Error</h1>
<p>The application could not run because of the following error:</p>
<h2>Details</h2>
<div><strong>Type:</strong> Error</div>
<div><strong>Message:</strong> Class 'JWT' not found</div>
<div><strong>File:</strong> C:\Users\Desktop\php\src\routes\user.php</div>
<div><strong>Line:</strong> 172</div>

Code on line 172 of user.php

$jwt = JWT::encode($token, $secret_key);

Code in my index.php

require __DIR__.'/./vendor/autoload.php';
use \Firebase\JWT\JWT;
require __DIR__.'/./src/config/db.php';

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

require_once __DIR__.'/./src/routes/user.php';

This is my autoload.php

<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit23b54112a5e426a79a0f306cc05fddce::getLoader();

Would appreciate any help I can get on this matter. Thanks in advance.

Upvotes: 1

Views: 2497

Answers (1)

Trelaquix
Trelaquix

Reputation: 83

Yup my code is now working when I add

use \Firebase\JWT\JWT

in my user.php file instead of the index.php.

Thanks @jumper85 for your answer.

Upvotes: 2

Related Questions