Get started with jsonwebtoken CDN

MIT licensed

JSON Web Token: Compact, URL-safe method for transferring claims between parties.

Tags:
  • jwt

Stable version

Copied!

How to start using jsonwebtoken CDN


const jwt = require('jsonwebtoken')({ signingKey: 'your-secret-key' }); // Initialize JWT with a secret key

// Function to generate a JWT token
function generateToken(payload) {
  const token = jwt.sign(payload, 'your-secret-key', { expiresIn: '1h' }); // Sign the payload with the secret key and set expiration time
  return token;
}

// Function to verify a JWT token
function verifyToken(token) {
  try {
    const decoded = jwt.verify(token, 'your-secret-key'); // Verify the token with the secret key
    return decoded;
  } catch (err) {
    return null;
  }
}

// Usage example
const payload = { sub: '1234567890', name: 'John Doe', iat: 1615137222 }; // Replace with your actual payload
const token = generateToken(payload);
console.log('Generated token:', token);

const decoded = verifyToken(token);
console.log('Decoded token:', decoded); // Should print the original payload
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!

All versions