cleanOldLogEntries(days = 7) { return new Promise(resolve => { const stmt = this.db.prepare('DELETE FROM logs WHERE created_at < $created_at'); stmt.run({ created_at: moment() .subtract(days, 'days') .unix() }); resolve(); }); }
async function saveUser(name, email, password) { const forgotPassword = { token: crypto.randomBytes(20).toString('hex'), createdAt: moment().unix() }; const user = new User({name, email, password, forgotPassword}); const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt); return await user.save(); }
function isAuth (req, res, next) { if (!req.headers.authorization) { return res.status(403).res({ message: `NO TIENES AUTORIZACION` }) } const token = req.headers.authorization.split(' ')[1] const payload = jwt.decode(token, config.SECRET_TOKEN) if (payload.exp < moment().unix()) { return res.status(401).send({ message: `TOKEN HA EXPIRADO` }) } req.user = payload.sub next() }
function getEanIssueSession(elecEan, gasEan, address, zipcode) { const disclosureContent = constructEanDisclosureContent(address, zipcode); const attributes = constructEanAttributes(elecEan, gasEan); return diva.startIssueSession([{ credential: 'irma-demo.alliander.connection', validity: moment().add(6, 'months').unix(), attributes, }], disclosureContent); }
// calling APIs const getPastTimeStamps = () => { const dates = [] for (let i = daysOfData; i > -1; i--) { dates.push(moment().subtract(i, 'days').unix()) } return _.chunk(dates, 15) }
function decodeToken(token, callback) { const payload = jwt.decode(token, process.env.TOKEN_SECRET); const now = moment().unix(); // check if the token has expired if (now > payload.exp) callback('Token has expired.'); else callback(null, payload); }
async set(key, value) { let time = +moment().unix() let profileStart = +moment() client.set(key, JSON.stringify({ value, time })) //Profile if (program.mode === 'profile') { let profileEnd = +moment() console.log(`[Profile] 缓存写入 ${profileEnd - profileStart} ms`) } }
function createToken(user) { var payload = { sub: user.id, iat: moment().unix(), exp: moment().add(365, 'days') } return jwt.encode(payload, secretKey, null, null); }
cleanOldLogEntries(days = 14) { return new Promise(resolve => { const stmt = this.db.prepare('DELETE FROM ticker_log WHERE income_at < $income_at'); stmt.run({ income_at: moment() .subtract(days, 'days') .unix() }); resolve(); }); }
async function saveUser(name, email, password) { const forgotPassword = { token: crypto.randomBytes(20).toString('hex'), createdAt: moment().unix() }; const user = new User({name, email, password, forgotPassword}); const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt); return await user.save(); }
async function saveUser(name, email, password) { const forgotPassword = { token: crypto.randomBytes(20).toString('hex'), createdAt: moment().unix() }; const user = new User({name, email, password, forgotPassword}); const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt); return await user.save(); }