kickstart-express
Version:
A powerful CLI tool to quickly scaffold Express.js projects with modern tooling and best practices
61 lines (51 loc) • 1.38 kB
JavaScript
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
/**
* JWT Utility Functions
* Helper functions for JWT token generation and password hashing
*/
/**
* Generate a JWT token for a user
* @param {Object} payload - User data to include in token
* @returns {string} JWT token
*/
export const generateToken = (payload) => {
return jwt.sign(
payload,
process.env.JWT_SECRET,
{
expiresIn: process.env.JWT_EXPIRES_IN || '7d',
issuer: 'kickstart-express'
}
);
};
/**
* Hash a password using bcrypt
* @param {string} password - Plain text password
* @returns {Promise<string>} Hashed password
*/
export const hashPassword = async (password) => {
const saltRounds = 12;
return await bcrypt.hash(password, saltRounds);
};
/**
* Compare a plain text password with a hash
* @param {string} password - Plain text password
* @param {string} hash - Hashed password
* @returns {Promise<boolean>} True if passwords match
*/
export const comparePassword = async (password, hash) => {
return await bcrypt.compare(password, hash);
};
/**
* Verify and decode a JWT token
* @param {string} token - JWT token
* @returns {Object|null} Decoded token payload or null if invalid
*/
export const verifyToken = (token) => {
try {
return jwt.verify(token, process.env.JWT_SECRET);
} catch (error) {
return null;
}
};