<?php

use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Config\JWT as JWTConfig;

function generateJWT(array $payload)
{
    $config = new JWTConfig();

    $issuedAt = time();
    $expire   = $issuedAt + $config->ttl;

    $token = [
        'iat'  => $issuedAt,
        'exp'  => $expire,
        'data' => $payload
    ];

    return JWT::encode($token, $config->secretKey, 'HS256');
}

function decodeJWT(string $token)
{
    $config = new JWTConfig();
    return JWT::decode($token, new Key($config->secretKey, 'HS256'));
}