Published on January 26, 2024By DeveloperBreeze

// Import 'jsonwebtoken' module
const jwt = require('jsonwebtoken');

// Secret key for signing and verifying JWT tokens
const secret = 'mysecretkey';

// Create a JWT token with a payload and set expiration to 1 hour
const payload = { user_id: 123, username: 'john_doe' };
const token = jwt.sign(payload, secret, { expiresIn: '1h' });

// Verify a JWT token
try {
    const decoded = jwt.verify(token, secret);
    console.log(decoded);
} catch (error) {
    console.error('Token verification failed.');
}

Comments

Please log in to leave a comment.

Continue Reading:

Simple WebSocket Server using 'ws' library

Published on January 26, 2024

javascript

File Stream Copy using 'fs' module

Published on January 26, 2024

javascript

Execute Shell Command using 'child_process' module

Published on January 26, 2024

javascript

Parse URL and Query Parameters

Published on January 26, 2024

javascript

Hashing Password with SHA-256 using 'crypto' module

Published on January 26, 2024

javascript

Create and Print Buffer

Published on January 26, 2024

javascript

Basic Authentication using 'express-basic-auth' middleware

Published on January 26, 2024

javascript

Construct File Path using 'path' module

Published on January 26, 2024

javascript

Event Emitter using 'events' module

Published on January 26, 2024

javascript

Set and Access Environment Variable

Published on January 26, 2024

javascript