I have a file named validationForgetPassword.js
with the following code:
"use strict"
var config = require('./config'),
createHash = require('sha.js'),
pgPromise = require('./pgPromise'),
winston = require('./winston');
module.exports = function (id, time, hashForgetPassword) {
var date = new Date();
date = date.getTime();
if (date > time) {
logger.log('error', 'date expirated');
return null;
}
pgPromise.db.oneOrNone('select dr_email, dr_tax_id, dr_license, dr_first_name, dr_last_name, dr_cell_phone from driver where dr_id = ($1)', [id])
.then(function (results) {
if (results) {
results.time = time;
var result = JSON.stringify(results);
var sha256 = createHash('sha256')
var hash = sha256.update(result, 'utf8').digest('hex');
console.log(hash);
console.log(hashForgetPassword);
if(hashForgetPassword === hash) {
return 1;
} else {
logger.log('error', 'hash is not valid');
return null;
}
} else {
logger.log('error', 'Not result in select driver');
return null;
}
}).catch(function (error) {
logger.log('error', 'Error in select driver from module validationForgetPassword \n' + error);
return null;
});
}
And a server.js
with the following code:
var express = require('express'),
bodyParser = require('body-parser'),
cors = require('cors');
path = require ('path');
var login = require('./api/routes/login'),
logoff = require('./api/routes/logoff'),
forgetPasswordUser = require('./api/routes/forgetPasswordUser');
importEvents = require('./api/routes/importEvents'),
taskWorker = require('./api/routes/taskWorker'),
create = require('./api/routes/create'),
validationForgetPassword = require('./config/validationForgetPassword');
winston = require('./config/winston');
var app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.set('views', __dirname + '/api/views');
new winston();
importEvents();
taskWorker();
var port = process.env.PORT || 8080;
app.use('/api/v1/driver/login', login);
app.use('/api/v1/driver/logoff', logoff);
app.use('/api/v1/driver/create', create);
app.use('/forgetPasswordUser', forgetPasswordUser);
app.get('/forgetPassword/:id/:time/:hash', function(req, res) {
const id = req.params.id;
const time = req.params.time;
const hash = req.params.hash;
var token;
token = validationForgetPassword(id, time, hash);
if (token == 1) {
res.render('pages/forgetPassword');
} else {
//res.render('pages/forgetPassword/oi');
}
});
app.listen(port);
console.log('API started on port ' + port);
However, the result returns undefined, because it executes if before the return.