Your IP : 216.73.216.48
const db = require("./db");
const bcrypt = require("bcrypt");
const { jwtGen } = require("./utils");
module.exports = (req, res) => {
const { identifier, password } = req.body;
db.query(
"SELECT * FROM users WHERE `identifier`=?",
[identifier],
(err, sqlRes) => {
if (err) throw err;
const user = sqlRes[0];
if (!!user) {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const userPassword = user.password;
bcrypt.compare(password, userPassword, (err, valid) => {
if (err) throw err;
db.query('INSERT INTO `log` (type, timestamp, meta, ip) VALUES ("login", ?, ?, ?)', [Math.floor(Date.now() / 1000), user.PIN, ip], err => {
if (err) throw err;
const jwt = jwtGen({ pin: user.PIN, identifier: user.identifier, type: user.type });
valid ? res.send(jwt) : res.status(401).send();
});
});
} else {
res.status(401).send();
}
}
);
};