Skip to content

Commit feb4285

Browse files
committed
Merge branch 'master' of https://github.com/code-squads/djcsi_code_squad into Vansh
2 parents 3d72c86 + 91b7b61 commit feb4285

File tree

13 files changed

+932
-419
lines changed

13 files changed

+932
-419
lines changed

package-lock.json

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"react-dom": "18.2.0",
3030
"react-otp-input": "^3.0.0",
3131
"react-spinners": "^0.13.8",
32+
"react-toastify": "^9.1.2",
3233
"tailwind-datepicker-react": "^1.2.3"
3334
},
3435
"devDependencies": {

server/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (process.env.NODE_ENV !== "production") {
1111
const { MONGODB_SRV_STRING, PORT } = require("./constants/config");
1212
const newEntity = require('./routes/newEntity');
1313
const auth = require('./routes/auth');
14+
const sms = require('./routes/sms');
1415

1516
// Setting the MongoDB
1617
mongoose.set('strictQuery', true);
@@ -35,7 +36,7 @@ app.listen(PORT, () => console.log(`Server is running on ${PORT}`));
3536

3637

3738
// Preparing routers
38-
app.use(newEntity, auth);
39+
app.use(newEntity, auth, sms);
3940

4041

4142

server/routes/sms.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
4+
const { sendOTP, verifyOTP } = require("../services/smsAPI");
5+
6+
// OTP routes
7+
router.post("/apis/sendOTP", (req, res) => {
8+
const phone = `+91${req.body.phone}`;
9+
10+
if (!phone)
11+
return res.status(400).send({
12+
message: "Wrong phone number :(",
13+
phone,
14+
success: false,
15+
data,
16+
});
17+
18+
sendOTP(phone)
19+
.then(() => {
20+
res.status(200).send({
21+
message: `OTP sent to ${phone}`
22+
})
23+
})
24+
.catch((err) => {
25+
console.log(err);
26+
console.log(`Some err sending OTP to ${phone}`);
27+
res.status(500).send({
28+
message: "Server error, contact administrator",
29+
phone,
30+
success: false,
31+
err,
32+
});
33+
});
34+
});
35+
36+
// Verify Endpoint
37+
router.post("/apis/verifyOTP", (req, res) => {
38+
const phone = `+91${req.body.phone}`;
39+
const code = req.body.code;
40+
41+
if (!phone || code.length != CODE_LENGTH)
42+
return res.status(400).send({
43+
message: "Invalid phone number or code :(",
44+
success: false,
45+
phone,
46+
code,
47+
});
48+
49+
verifyOTP(phone, code)
50+
.then((valid) => {
51+
if (valid) {
52+
console.log(`OTP approved for ${phone}`);
53+
return res.status(200).send({
54+
message: "OTP is Verified successfuly!!",
55+
success: true,
56+
});
57+
}
58+
return res.status(203).send({
59+
message: "Wrong code",
60+
success: false,
61+
});
62+
})
63+
.catch((error) => {
64+
console.log(error);
65+
console.log(
66+
`Some err verifying OTP ${code} for ${phone}, maybe it is already verified`
67+
);
68+
res.status(500).send({
69+
message: "Server error, contact administrator",
70+
phone,
71+
code,
72+
success: false,
73+
error,
74+
});
75+
});
76+
});
77+
78+
79+
module.exports = router;

server/services/smsAPI.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID;
2+
const AUTH_TOKEN = process.env.AUTH_TOKEN;
3+
const SERVICE_ID = process.env.SERVICE_ID;
4+
5+
console.log("Twilio params", {
6+
TWILIO_ACCOUNT_SID,
7+
AUTH_TOKEN,
8+
SERVICE_ID
9+
});
10+
11+
const twilioClient = require('twilio')(TWILIO_ACCOUNT_SID, AUTH_TOKEN);
12+
13+
exports.sendOTP = (phone) => {
14+
console.log("Hello")
15+
return new Promise((resolve, reject) => {
16+
twilioClient.verify.v2.services(SERVICE_ID)
17+
.verifications
18+
.create({to: phone, channel: 'sms'})
19+
.then(resolve)
20+
.catch(reject)
21+
})
22+
}
23+
24+
exports.verifyOTP = (phone, code) => {
25+
return new Promise((resolve, reject) => {
26+
client.verify.v2.services(SERVICE_ID)
27+
.verificationChecks
28+
.create({to: phone, code: code})
29+
.then(resolve)
30+
.catch(reject);
31+
})
32+
}

0 commit comments

Comments
 (0)