How to fix error "Class 'App\\Db\\Database' not found" on slim framework

Viewed 223

I am trying to make API call using postman from the project I uploaded online (cpanel shared hosting) but kept getting this error:

{
"statusCode": 500,
"error": {
    "type": "SERVER_ERROR",
    "description": "Class 'App\\Db\\Database' not found"
   }
}

Here is my controller trying to test Login API when I make a request:

namespace App\Controllers;

use App\Db\Database;
use Firebase\JWT\JWT;
use Respect\Validation\Exceptions\NestedValidationException;
use Slim\Psr7\Response;
use Respect\Validation\Validator as v;

class LoginController
{
public function __construct($data, Response $response)
{
    $validator = v::keySet(
        v::key('username', v::stringType()->notEmpty()->setName('username')->length(2, 100)),
        v::key('password', v::stringType()->notEmpty()->setName('password'))
    );

    try {
        $validator->assert($data);
    } catch (NestedValidationException $e) {
        $response->getBody()->write(json_encode([
            'responseCode' => "13",
            'responseMessage' => $e->getMessages(),
        ]));
        return $response->withHeader('Content-Type', 'application/json');
    }

    $username = $data['username'];
    $password = $data['password'];

    $hash_pwd = password_hash($password, PASSWORD_DEFAULT);
    $db = Database::getInstance();

    $login_query =
        $db->select(
            'users',
            [
                'email_address',
                'passwords'
            ],
            [
                'email_address' => $username,
            ]
        );

    if (!$login_query) {
        $response->getBody()->write(json_encode([
            'responseCode' => '99',
            'responseMessage' => "Invalid Login Details"
        ]));
    } else {
        $user_hash_pwd = $login_query[0]['passwords'];
        $password_verify = password_verify($password, $user_hash_pwd);
        if ($password_verify) {
            $key = "!@#$%^&*()_+";
            $payload = array(
                "iss" => "http://localhost",
                "aud" => "http://localhost",
                'iat' => time(),
                'uid' => 1,
                'exp' => time() + 10,
            );
            $token = JWT::encode($payload, $key);
            $decoded = JWT::decode($token, $key, array('HS256'));


            $response->getBody()->write(json_encode([
                'responseCode' => '0',
                'responseMessage' => "Logged In",
                'responseBody' => [
                    'token' => $token
                ]
            ]));
        } else {
            $response->getBody()->write(json_encode([
                'responseCode' => '99',
                'responseMessage' => "Incorrect password"
            ]));
        }
    }
 }

}

The endpoint is api/v1/login. Though the database connection details are correct and the namespace too. Please how can I fix this?

0 Answers
Related