-
Notifications
You must be signed in to change notification settings - Fork 4
/
otp.js
47 lines (44 loc) · 1.48 KB
/
otp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require('dotenv').config()
const mqtt = require('./mqtt')
const redis = require('./redis')
const { v4:uuid4 } = require('uuid');
const logger = require('./logger')
const requestOTP = (req,res) => {
const {PORT} = process.env
const {phoneNumber} = req.body;
if(phoneNumber == '' || phoneNumber == undefined){
res.send(JSON.stringify({ok:0,message:"Payload cannot be null"}))
return
}
const otp = Math.floor(1000 + Math.random() * 9000);
const id = uuid4()
const payload = JSON.stringify({id:id,phoneNumber:phoneNumber,otp:otp,message:`OTP Code = ${otp}`})
redis.set(id,payload,60)
mqtt.publish('2pai-dev/pulsa',payload)
logger.info('request-otp',payload);
res.send(JSON.stringify({ok:1,message:"OK",next:`http://${req.hostname}:${PORT}/verifyOtp/${id}`}))
}
const verifyOTP = (req,res) => {
const {otpCode} = req.body;
const {id} = req.params;
if(otpCode == '' || otpCode == undefined){
res.send(JSON.stringify({ok:0,message:"Payload cannot be null"}))
return
}
redis.client.get(id,(err,data) => {
if(data){
let parsed = JSON.parse(data);
if (parsed.otp == otpCode){
res.send(JSON.stringify({ok:1,message:"OTP VALID"}))
}else{
res.send(JSON.stringify({ok:0,message:"OTP Invlaid"}))
}
}else{
res.send(JSON.stringify({ok:0,message:"Error!"}))
}
})
}
module.exports = {
requestOTP,
verifyOTP
}