37 lines
763 B
JavaScript
37 lines
763 B
JavaScript
|
|
const bcrypt = require('bcryptjs');
|
||
|
|
const jwt = require('jsonwebtoken');
|
||
|
|
|
||
|
|
// Hash password with bcrypt
|
||
|
|
async function hashPassword(password) {
|
||
|
|
const salt = await bcrypt.genSalt(10);
|
||
|
|
return bcrypt.hash(password, salt);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Compare password with hash
|
||
|
|
async function comparePassword(password, hash) {
|
||
|
|
return bcrypt.compare(password, hash);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Generate JWT token
|
||
|
|
function generateToken(payload) {
|
||
|
|
return jwt.sign(payload, process.env.JWT_SECRET, {
|
||
|
|
expiresIn: process.env.JWT_EXPIRES_IN || '24h',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Verify JWT token
|
||
|
|
function verifyToken(token) {
|
||
|
|
try {
|
||
|
|
return jwt.verify(token, process.env.JWT_SECRET);
|
||
|
|
} catch (error) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
hashPassword,
|
||
|
|
comparePassword,
|
||
|
|
generateToken,
|
||
|
|
verifyToken,
|
||
|
|
};
|