Skip to content

Commit d355260

Browse files
RohanRohan
authored andcommitted
server js fixed
1 parent d766cec commit d355260

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

server/apis/smsAPI.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const SERVICE_ID = process.env.SERVICE_ID;
2+
const TWILIO_ACCOUNT_SID = process.env.ACCOUNT_SID;
3+
const AUTH_TOKEN = process.env.AUTH_TOKEN;
4+
const OTP = require('../dbModels/otp')
5+
6+
// const twilioClient = require('twilio')(TWILIO_ACCOUNT_SID, AUTH_TOKEN);
7+
const fast2sms = require('fast-two-sms')
8+
9+
exports.sendOTP = async (phone) => {
10+
phone = parseInt(phone.substring(3))
11+
var otp = Math.floor(100000 + Math.random() * 9000);
12+
13+
const numberExists = await OTP.findOne({phone:phone})
14+
if(numberExists) {
15+
numberExists.otp = otp
16+
await numberExists.save()
17+
}
18+
else {
19+
const newOTP = new OTP({
20+
phone: phone,
21+
otp: otp
22+
})
23+
await newOTP.save()
24+
}
25+
var options = {
26+
authorization: process.env.FAST_TWO_SMS_KEY,
27+
message: `${otp} is your OTP -MedBlock`,
28+
numbers: [phone]
29+
}
30+
try {
31+
fast2sms.sendMessage(options)
32+
} catch(error) {
33+
console.log(error)
34+
}
35+
return otp
36+
}
37+
38+
exports.verifyOTP = async (phone, code) => {
39+
phone = parseInt(phone.substring(3))
40+
const otp = await OTP.findOne({phone:phone})
41+
if(otp) {
42+
if(parseInt(code) === otp.otp) {
43+
otp.delete()
44+
return(true)
45+
}
46+
else
47+
return(false)
48+
}
49+
else {
50+
return(false)
51+
}
52+
}
53+
54+
55+
56+
57+
// exports.sendOTP = phone => {
58+
// return new Promise((resolve, reject) => {
59+
// twilioClient
60+
// .verify
61+
// .services(SERVICE_ID)
62+
// .verifications
63+
// .create({
64+
// to: phone,
65+
// channel: 'sms'
66+
// })
67+
// .then(resolve)
68+
// .catch(reject);
69+
// })
70+
// }
71+
72+
// exports.verifyOTP = (phone, code) => {
73+
// return new Promise((resolve, reject) => {
74+
// twilioClient
75+
// .verify
76+
// .services(SERVICE_ID)
77+
// .verificationChecks
78+
// .create({
79+
// to: phone,
80+
// code
81+
// })
82+
// .then(data => {
83+
// if(data.status === "approved")
84+
// resolve(true);
85+
// else
86+
// resolve(false);
87+
// })
88+
// .catch(reject);
89+
// })
90+
// }

server/dbModels/otp.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mongoose = require("mongoose")
2+
3+
const Schema = mongoose.Schema
4+
5+
const OTP = new Schema({
6+
phone: {
7+
type: Number,
8+
required: true
9+
},
10+
otp: {
11+
type: Number,
12+
required: true
13+
}
14+
})
15+
16+
module.exports = mongoose.model("OTP", OTP)

server/src/index.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const express = require("express");
2+
const slashes = require("connect-slashes");
3+
const cors = require("cors");
4+
const mongoose = require("mongoose");
5+
6+
const { sendOTP, verifyOTP } = require("../apis/smsAPI");
7+
8+
9+
const PORT = process.argv[2] || process.env.PORT || 5000;
10+
const CODE_LENGTH = process.env.CODE_LENGTH;
11+
12+
// Setting up the server
13+
const app = express();
14+
app.use(cors());
15+
app.use(express.json());
16+
app.use(express.urlencoded({ extended: true }));
17+
app.use(slashes(false));
18+
app.listen(PORT, () =>
19+
console.log(`server is up and running on port ${PORT}`)
20+
);
21+
22+
// Setting the MongoDB
23+
mongoose
24+
.connect(process.env.MONGODB_SRV_STRING)
25+
.then((success) => {
26+
console.log("Successfuly connected to MongoDB !!");
27+
})
28+
.catch((err) => {
29+
console.log("Error in mongoose connection !");
30+
console.log(err);
31+
});
32+
33+
// OTP routes
34+
app.post("/apis/sendOTP", (req, res) => {
35+
const phone = `+91${req.body.phone}`;
36+
37+
if (!phone)
38+
return res.status(400).send({
39+
message: "Wrong phone number :(",
40+
phone,
41+
success: false,
42+
data,
43+
});
44+
45+
sendOTP(phone)
46+
.then((data) => {
47+
console.log(data);
48+
console.log(`OTP sent to ${phone}`);
49+
res.status(200).send({
50+
51+
message: "OTP is sent successfuly !!",
52+
phone,
53+
success: true,
54+
data,
55+
});
56+
})
57+
.catch((err) => {
58+
console.log(err);
59+
console.log(`Some err sending OTP to ${phone}`);
60+
res.status(500).send({
61+
message: "Server error, contact administrator",
62+
phone,
63+
success: false,
64+
error,
65+
});
66+
});
67+
});
68+
69+
// Verify Endpoint
70+
app.post("/apis/verifyOTP", (req, res) => {
71+
const phone = `+91${req.body.phone}`;
72+
const code = req.body.code;
73+
74+
if (!phone || code.length != CODE_LENGTH)
75+
return res.status(400).send({
76+
message: "Invalid phone number or code :(",
77+
success: false,
78+
phone,
79+
code,
80+
});
81+
82+
verifyOTP(phone, code)
83+
.then((valid) => {
84+
if (valid) {
85+
console.log(`OTP approved for ${phone}`);
86+
return res.status(200).send({
87+
message: "OTP is Verified successfuly!!",
88+
success: true,
89+
});
90+
}
91+
return res.status(203).send({
92+
message: "Wrong code",
93+
success: false,
94+
});
95+
})
96+
.catch((error) => {
97+
console.log(error);
98+
console.log(
99+
`Some err verifying OTP ${code} for ${phone}, maybe it is already verified`
100+
);
101+
res.status(500).send({
102+
message: "Server error, contact administrator",
103+
phone,
104+
code,
105+
success: false,
106+
error,
107+
});
108+
});
109+
});
110+
111+
app.get("/responses/ping", (req, res) => {
112+
res.status(200).send("-- ok --");
113+
});
114+
115+
// 404 pages for development
116+
app.get("*", (req, res) => {
117+
res.status(404).send("API not found :( <br> ¯\\_(ツ)_/¯");
118+
});

0 commit comments

Comments
 (0)