diff options
author | rohan09-raj <rajrohan1914@gmail.com> | 2022-08-19 00:10:29 +0530 |
---|---|---|
committer | rohan09-raj <rajrohan1914@gmail.com> | 2022-08-19 00:10:42 +0530 |
commit | 953dd3c954411d92811415cc9be4a7c7d76ab185 (patch) | |
tree | 0bd2aa118361bbe70f0ac0b62da3c12fa1cb902f /server | |
parent | a9ed2c9daa5a3e065401996daf922f59de7f0101 (diff) |
added otp support
Diffstat (limited to 'server')
-rw-r--r-- | server/.env | 5 | ||||
-rw-r--r-- | server/constants/aadhaar.js | 1 | ||||
-rw-r--r-- | server/controllers/otp.js | 17 | ||||
-rw-r--r-- | server/controllers/users.js | 2 | ||||
-rw-r--r-- | server/index.js | 2 | ||||
-rw-r--r-- | server/routes/otp.js | 8 | ||||
-rw-r--r-- | server/services/twilio.js | 4 | ||||
-rw-r--r-- | server/utils/otp.js | 8 |
8 files changed, 42 insertions, 5 deletions
diff --git a/server/.env b/server/.env index 586c878..3bce271 100644 --- a/server/.env +++ b/server/.env @@ -4,4 +4,7 @@ PORT = 5000 //TWILIO TWILIO_ACCOUNT_SID = AC77501e433321b984614102ea5d611580 TWILIO_AUTH_TOKEN = c6823da64d024efcced488b482c1865f -TWILIO_PHONE_NUMBER = +14235645815
\ No newline at end of file +TWILIO_PHONE_NUMBER = +14235645815 + +//SENDGRID +SENDGRID_API_KEY = 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
\ No newline at end of file diff --git a/server/constants/aadhaar.js b/server/constants/aadhaar.js index b4405e8..6bfee5b 100644 --- a/server/constants/aadhaar.js +++ b/server/constants/aadhaar.js @@ -1 +1,2 @@ export const ELEVEN_TIMES_NINE = 99999999999; +export const SIX_TIMES_NINE = 999999; diff --git a/server/controllers/otp.js b/server/controllers/otp.js new file mode 100644 index 0000000..5b4bd82 --- /dev/null +++ b/server/controllers/otp.js @@ -0,0 +1,17 @@ +import generateOTP from '../utils/otp'; +import sendMessage from '../services/twilio'; + +const sendOTP = async (req, res) => { + const {mobile} = req.body; + + try { + const otp = generateOTP(); + const message = `Your OTP for Aadhaar verification is : ${otp}`; + sendMessage(mobile, message); + res.status(200).json({message: 'OTP sent successfully', otpCode: otp}); + } catch (error) { + res.status(404).json({message: error}); + } +}; + +export default {sendOTP}; diff --git a/server/controllers/users.js b/server/controllers/users.js index 33d2ef0..814ea44 100644 --- a/server/controllers/users.js +++ b/server/controllers/users.js @@ -1,5 +1,4 @@ import UserDetails from '../models/users'; -import sendOTP from '../services/twilio'; import generateAadhaar from '../utils/aadhaar'; const createUser = async (req, res) => { @@ -19,7 +18,6 @@ const createUser = async (req, res) => { try { const existingUser = await UserDetails.findOne({name, mobile, email}); if (existingUser) { - // sendOTP('+919696712475', 'Hi, I am Rohan Raj Gupta'); return res.status(400).json({message: 'User already exists.'}); } diff --git a/server/index.js b/server/index.js index 0d058ae..2ccd121 100644 --- a/server/index.js +++ b/server/index.js @@ -6,6 +6,7 @@ import dotenv from 'dotenv'; import userRoutes from './routes/users.js'; import healthRoutes from './routes/health.js'; +import otpRoutes from './routes/otp.js'; const app = Express(); dotenv.config(); @@ -16,6 +17,7 @@ app.use(cors()); app.use('/', healthRoutes); app.use('/', userRoutes); +app.use('/', otpRoutes); const CONNECTION_URL = process.env.CONNECTION_URL; const PORT = process.env.PORT; diff --git a/server/routes/otp.js b/server/routes/otp.js new file mode 100644 index 0000000..b73240e --- /dev/null +++ b/server/routes/otp.js @@ -0,0 +1,8 @@ +import Express from 'express'; +import otp from '../controllers/otp'; + +const router = Express.Router(); + +router.post('/otp', otp.sendOTP); + +export default router; diff --git a/server/services/twilio.js b/server/services/twilio.js index 06c88e5..f95cc5f 100644 --- a/server/services/twilio.js +++ b/server/services/twilio.js @@ -3,7 +3,7 @@ import twilio from 'twilio'; const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; -const sendOTP = (phone, message) => { +const sendMessage = (phone, message) => { const client = twilio(accountSid, authToken); client.messages .create({ @@ -14,4 +14,4 @@ const sendOTP = (phone, message) => { .then((message) => console.log(message.sid)); }; -export default sendOTP; +export default sendMessage; diff --git a/server/utils/otp.js b/server/utils/otp.js new file mode 100644 index 0000000..9a2a0bc --- /dev/null +++ b/server/utils/otp.js @@ -0,0 +1,8 @@ +import {SIX_TIMES_NINE} from '../constants/aadhaar'; + +const generateOTP = () => { + const otp = Math.floor(Math.random() * SIX_TIMES_NINE + 1); + return otp; +}; + +export default generateOTP; |