Created
February 13, 2023 14:12
-
-
Save tushar4303/615b1a39d8ff24a964019c01b7884e11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const mysql = require('mysql'); | |
const msg91 = require('msg91')('YOUR_API_KEY', 'YOUR_SENDER_ID', 'ROUTE_NO'); | |
const app = express(); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
const connection = mysql.createConnection({ | |
host: 'localhost', | |
user: 'YOUR_USERNAME', | |
password: 'YOUR_PASSWORD', | |
database: 'YOUR_DATABASE' | |
}); | |
app.post('/sendotp', (req, res) => { | |
const mobileNumber = req.body.mobileNumber; | |
const otp = Math.floor(100000 + Math.random() * 900000); | |
const insertQuery = `INSERT INTO otp (mobile_number, otp, created_at) VALUES (${mobileNumber}, ${otp}, NOW())`; | |
connection.query(insertQuery, (insertError, insertResults, insertFields) => { | |
if (insertError) { | |
console.error(insertError); | |
res.status(500).send('Internal Server Error'); | |
} else { | |
const message = `Your OTP for verification is ${otp}. Do not share it with anyone. -Your Company Name`; | |
msg91.send(mobileNumber, message, (sendError, response) => { | |
if (sendError) { | |
console.error(sendError); | |
res.status(500).send('Failed to send OTP'); | |
} else { | |
console.log(`OTP sent to ${mobileNumber}`); | |
res.status(200).send('OTP sent successfully'); | |
} | |
}); | |
} | |
}); | |
}); | |
app.post('/validateotp', (req, res) => { | |
const mobileNumber = req.body.mobileNumber; | |
const enteredOtp = req.body.otp; | |
const validateOtp = () => { | |
const otpQuery = `SELECT otp, created_at FROM otp WHERE mobile_number=${mobileNumber}`; | |
connection.query(otpQuery, (otpError, otpResults, otpFields) => { | |
if (otpError) { | |
console.error(otpError); | |
res.status(500).send('Internal Server Error'); | |
connection.end(); | |
return; | |
} | |
const dbOtp = otpResults[0].otp; | |
const createdAt = new Date(otpResults[0].created_at); | |
const now = new Date(); | |
const diff = (now.getTime() - createdAt.getTime()) / 1000; | |
if (dbOtp != enteredOtp) { | |
const retryCountQuery = `SELECT retry_count FROM otp WHERE mobile_number=${mobileNumber}`; | |
connection.query(retryCountQuery, (retryCountError, retryCountResults, retryCountFields) => { | |
if (retryCountError) { | |
console.error(retryCountError); | |
res.status(500).send('Internal Server Error'); | |
connection.end(); | |
return; | |
} | |
res.status(400).send(`Invalid OTP. You have ${3 - retryCountResults[0].retry_count} attempts left`); | |
if (retryCountResults[0].retry_count >= 2) { | |
const blockQuery = `UPDATE otp SET is_blocked=1 WHERE mobile_number=${mobileNumber}`; | |
connection.query(blockQuery, (blockError, blockResults, blockFields) => { | |
if (blockError) { | |
console.error(blockError); | |
} else { | |
console.log('OTP blocked'); | |
} | |
}); | |
} else { | |
const retryCount = retryCountResults[0].retry_count + 1; | |
const updateQuery = `UPDATE otp SET retry_count=${retryCount} WHERE mobile_number=${mobileNumber}`; | |
connection.query(updateQuery, (updateError, updateResults, updateFields) => { | |
if (updateError) { | |
console.error(updateError); | |
} else { | |
console.log(`OTP retry count updated to ${retryCount}`); | |
} | |
}); | |
} | |
connection.end(); | |
} | |
}); | |
}); | |
} | |
}); | |
}; | |
const blockQuery = `SELECT is_blocked FROM otp WHERE mobile_number=${mobileNumber}`; | |
connection.query(blockQuery, (blockError, blockResults, blockFields) => { | |
if (blockError) { | |
console.error(blockError); | |
res.status(500).send('Internal Server Error'); | |
connection.end(); | |
return; | |
} | |
const isBlocked = blockResults[0].is_blocked; | |
if (isBlocked) { | |
res.status(400).send('OTP blocked. Try again in 5 minutes'); | |
connection.end(); | |
} else { | |
validateOtp(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment