<?php

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$jwt_secret_key = 'n1Bbue6ZvjyzplD+B9v4ADZ8gbzAv4qiT+HIMEJtYWs=';
define('JWT_SECRET_KEY', $jwt_secret_key);


class JWTHandler {
    
    //const JWT_SECRET_KEY = bin2hex(random_bytes(32));
    
    public static function generateToken($userData) {
        $issuedAt = time();
        $expirationTime = $issuedAt + 360000;  // Token expires in 1 hour
        $payload = array(
            'iat' => $issuedAt,
            'exp' => $expirationTime,
            'data' => $userData
        );

        return JWT::encode($payload, JWT_SECRET_KEY, 'HS256');
    }

    public static function validateToken($token) {
        try {
            $decoded = JWT::decode($token, new Key(JWT_SECRET_KEY, 'HS256'));
            return $decoded->data;
        } catch (Exception $e) {
            return false;
        }
    }
}