Is storing an API key in the database a secure method for verifying user access to a NodeJS socket in a PHP application?

Storing an API key in the database for verifying user access to a NodeJS socket in a PHP application is not a secure method. It is recommended to use a more secure method such as JWT (JSON Web Tokens) for authentication and authorization between the PHP application and the NodeJS socket.

// Example of using JWT for authentication in PHP

// Install the firebase/php-jwt library using Composer
// composer require firebase/php-jwt

require_once 'vendor/autoload.php';

use \Firebase\JWT\JWT;

$secret_key = "your_secret_key";
$token = array(
    "user_id" => 123,
    "username" => "exampleuser"
);

$jwt = JWT::encode($token, $secret_key);
$decoded = JWT::decode($jwt, $secret_key, array('HS256'));

print_r($decoded);