export const parse = (str = '..') => JSON.parse(
    Buffer
        .from(
            str.split('.')[1] || 'e30=',
            'base64'
        )
        .toString('utf8')
);

export const stringify = (payload = {}) => [
    Buffer.from(JSON.stringify({ alg: 'none', type: 'JWT' }), 'utf8').toString('base64'),
    Buffer.from(JSON.stringify(payload), 'utf8').toString('base64'),
    ''
].join('.');

export const isExpired = (jwt) => {
    if (typeof jwt === 'string') {
        jwt = parse(jwt);
    }
    return (jwt.exp ?? 0) * 1000 < Date.now();
};
