Spaces:
Sleeping
Sleeping
File size: 1,459 Bytes
839aef2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import { settings } from '../config.js';
// passlib bcrypt hashes ($2b$/$2a$) are fully compatible with bcryptjs.
export function verifyPassword(plain, hashed) {
if (!hashed) return false;
try {
return bcrypt.compareSync(plain, hashed);
} catch {
return false;
}
}
export function getPasswordHash(password) {
return bcrypt.hashSync(password, 12);
}
function sign(data, expiresInSeconds, type) {
const payload = { ...data, type };
return jwt.sign(payload, settings.SECRET_KEY, {
algorithm: settings.ALGORITHM,
expiresIn: expiresInSeconds,
});
}
export function createAccessToken(data, expiresMinutes = settings.ACCESS_TOKEN_EXPIRE_MINUTES) {
return sign(data, expiresMinutes * 60, 'access');
}
export function createRefreshToken(data) {
return sign(data, settings.REFRESH_TOKEN_EXPIRE_DAYS * 24 * 60 * 60, 'refresh');
}
/** Returns the decoded payload or throws { status, detail }. */
export function decodeToken(token) {
try {
return jwt.verify(token, settings.SECRET_KEY, { algorithms: [settings.ALGORITHM] });
} catch {
const err = new Error('Could not validate credentials');
err.status = 401;
err.detail = 'Could not validate credentials';
throw err;
}
}
export default {
verifyPassword,
getPasswordHash,
createAccessToken,
createRefreshToken,
decodeToken,
};
|