Fix OTP bug

This commit is contained in:
Moe Poi ~ 2022-04-07 11:49:21 +07:00
parent 38dc5981fd
commit 553311434a

View file

@ -72,6 +72,7 @@ router.post("/register", async (req, res) => {
req.body.password, req.body.password,
saltRounds saltRounds
); );
let otptoken = randtoken.generate(64);
var users = { var users = {
first_name: req.body.first_name, first_name: req.body.first_name,
last_name: req.body.last_name, last_name: req.body.last_name,
@ -80,6 +81,7 @@ router.post("/register", async (req, res) => {
session: "[]", session: "[]",
otp: 0, otp: 0,
otpcode: 0, otpcode: 0,
otptoken: otptoken,
otpto: 0, otpto: 0,
otpservice: "", otpservice: "",
}; };
@ -198,6 +200,7 @@ router.post("/login", async (req, res) => {
}); });
} else { } else {
if (response[0].otp == 1) { if (response[0].otp == 1) {
let otptoken = randtoken.generate(64);
let otpcode = Math.floor(100000 + Math.random() * 900000); let otpcode = Math.floor(100000 + Math.random() * 900000);
telegram.send( telegram.send(
response[0].otpto, response[0].otpto,
@ -206,6 +209,7 @@ router.post("/login", async (req, res) => {
conn.query( conn.query(
'UPDATE users SET ? WHERE email ="' + req.body.email + '"', { 'UPDATE users SET ? WHERE email ="' + req.body.email + '"', {
otpcode: otpcode, otpcode: otpcode,
otptoken: otptoken,
}, },
function (err, result) { function (err, result) {
if (err) { if (err) {
@ -218,7 +222,7 @@ router.post("/login", async (req, res) => {
res.json({ res.json({
message: "OTP Verification Sent ~", message: "OTP Verification Sent ~",
otp: response[0].otp == 1 ? true : false, otp: response[0].otp == 1 ? true : false,
token: response[0].token token: otptoken,
}); });
} }
db.disconnect(conn); db.disconnect(conn);
@ -289,7 +293,7 @@ router.post("/otp-submit", async (req, res) => {
} else { } else {
const conn = db.connect(); const conn = db.connect();
conn.query( conn.query(
"SELECT * FROM users WHERE token = ?", "SELECT * FROM users WHERE otptoken = ?",
[req.body.token], [req.body.token],
async function (error, response, fields) { async function (error, response, fields) {
if (!response[0]) { if (!response[0]) {
@ -321,7 +325,7 @@ router.post("/otp-submit", async (req, res) => {
'UPDATE users SET ? WHERE otpcode ="' + req.body.code + '"', { 'UPDATE users SET ? WHERE otpcode ="' + req.body.code + '"', {
session: JSON.stringify(session), session: JSON.stringify(session),
otpcode: 0, otpcode: 0,
token: randtoken.generate(64) otptoken: randtoken.generate(64)
}, },
function (err, result) { function (err, result) {
if (err) { if (err) {
@ -393,6 +397,7 @@ router.post("/otp-toggle", async (req, res) => {
res.status(200); res.status(200);
res.json({ res.json({
message: `Success set OTP to ${otp}`, message: `Success set OTP to ${otp}`,
otp: otp,
}); });
} }
db.disconnect(conn); db.disconnect(conn);