summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrohan09-raj <rajrohan1914@gmail.com>2022-08-19 00:10:29 +0530
committerrohan09-raj <rajrohan1914@gmail.com>2022-08-19 00:10:42 +0530
commit953dd3c954411d92811415cc9be4a7c7d76ab185 (patch)
tree0bd2aa118361bbe70f0ac0b62da3c12fa1cb902f
parenta9ed2c9daa5a3e065401996daf922f59de7f0101 (diff)
added otp support
-rw-r--r--client/src/pages/Enrollment/Agreement/Agreement.jsx30
-rw-r--r--client/src/services/apiservice.js5
-rw-r--r--server/.env5
-rw-r--r--server/constants/aadhaar.js1
-rw-r--r--server/controllers/otp.js17
-rw-r--r--server/controllers/users.js2
-rw-r--r--server/index.js2
-rw-r--r--server/routes/otp.js8
-rw-r--r--server/services/twilio.js4
-rw-r--r--server/utils/otp.js8
10 files changed, 74 insertions, 8 deletions
diff --git a/client/src/pages/Enrollment/Agreement/Agreement.jsx b/client/src/pages/Enrollment/Agreement/Agreement.jsx
index 9764110..d5690f3 100644
--- a/client/src/pages/Enrollment/Agreement/Agreement.jsx
+++ b/client/src/pages/Enrollment/Agreement/Agreement.jsx
@@ -1,13 +1,33 @@
-import React from 'react'
+import React, { useState } from 'react'
import Header from '../../../components/Header/Header'
import CardAgreement from '../../../components/Card/CardAgreement'
import styles from './Agreement.module.css'
import Input from '../../../components/Input/Input'
import { Grid, Button } from '@mui/material'
import { useTranslation } from 'react-i18next'
+import { sendOTP } from '../../../services/apiservice'
+import { userContext } from '../../../context/User'
+import { useMutation } from 'react-query'
const Agreement = () => {
const { t } = useTranslation()
+ const [otp, setOtp] = useState(0)
+ const { userData } = userContext()
+ console.log(userData)
+ const { data, mutate } = useMutation(() =>
+ sendOTP({ mobile: `+91${userData?.mobile}` })
+ )
+
+ const verifyOTP = () => {
+ if (data?.data?.otpCode === Number(otp)) {
+ console.log('Verified', data?.data?.otpCode, otp)
+ } else {
+ console.log(typeof otp)
+ console.log(typeof data?.data?.otpCode)
+ console.log('Incorrect OTP', data?.data?.otpCode, otp)
+ }
+ }
+
return (
<>
<Header subheading={t('ENROLLMENT')} />
@@ -17,9 +37,11 @@ const Agreement = () => {
/>
</div>
<Input
- type="text"
+ type="number"
id="otp"
+ value={otp}
label={t('PLEASE_VERIFY_YOUR_IDENTITY')}
+ onChange={(e) => setOtp(e.target.value)}
placeholder="XXXXXX"
/>
<Grid container columnSpacing={10} justifyContent="center">
@@ -29,6 +51,7 @@ const Agreement = () => {
size="large"
type="submit"
variant="contained"
+ onClick={() => mutate()}
>
{t('SEND_OTP')}
</Button>
@@ -39,8 +62,9 @@ const Agreement = () => {
size="large"
type="submit"
variant="contained"
+ onClick={() => verifyOTP()}
>
- {t('RESEND')}
+ {t('VERIFY_OTP')}
</Button>
</Grid>
</Grid>
diff --git a/client/src/services/apiservice.js b/client/src/services/apiservice.js
index 305d557..0aa9337 100644
--- a/client/src/services/apiservice.js
+++ b/client/src/services/apiservice.js
@@ -18,3 +18,8 @@ export const updateUser = async (id, payload) => {
const response = await apiClient.patch(`/user/${id}`, payload)
return response
}
+
+export const sendOTP = async (payload) => {
+ const response = await apiClient.post('/otp', payload)
+ return response
+}
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;