-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ACCOUNT_SID = "AC2102664bca8f72c22aad5c193be70e2e" | ||
AUTH_TOKEN = "879a192516f57e7649f0500a83ffafe3" | ||
SERVICE_SID= "VA63bc7541a8ff566cd7ed42e4a7311df3" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
USER_COLLECTION: "users", | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const mongoClient = require("mongodb").MongoClient; | ||
const state = { | ||
db: null, | ||
}; | ||
module.exports.connect = (done) => { | ||
const url = | ||
"mongodb+srv://arnhss:rWwic2J417syd83l@cluster0.nrpdda3.mongodb.net/?retryWrites=true&w=majority"; | ||
const dbname = "marketfeed"; | ||
mongoClient.connect(url, { useUnifiedTopology: true }, (err, data) => { | ||
if (err) return done(err); | ||
state.db = data.db(dbname); | ||
done(); | ||
}); | ||
}; | ||
module.exports.get = () => { | ||
return state.db; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const otp = require("../utils/otp.util"); | ||
const userAuth = require("../helpers/auth/user.auth.js"); | ||
const jwt = require("../utils/jwt.util"); | ||
const { validationResult } = require("express-validator"); | ||
|
||
module.exports = { | ||
sendOtp: async (req, res) => { | ||
console.log("-> send otp route"); | ||
|
||
const errors = validationResult(req); | ||
// console.log(errors); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array(), status: false }); | ||
} | ||
|
||
try { | ||
otp | ||
.sendOtp(req.body.phone, req.body.channel) | ||
.then((response) => { | ||
console.log(response); | ||
res.json(response); | ||
}) | ||
.catch((error) => { | ||
res.json(error); | ||
// console.log("error HERE"); | ||
}); | ||
} catch (error) { | ||
// console.log("error"); | ||
res.json(error); | ||
} | ||
}, | ||
|
||
|
||
|
||
verifyOtp: async (req, res) => { | ||
console.log("-> verify otp route"); | ||
const errors = validationResult(req); | ||
console.log(errors); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array(), status: false }); | ||
} | ||
try { | ||
otp | ||
.verifyOtp(req.body.phone, req.body.otp) | ||
.then((response) => { | ||
userAuth | ||
.checkUser(req.body.phone) | ||
.then(async (data) => { | ||
// res.json(response); | ||
if (response.status) { | ||
if (data.status) { | ||
const token = await jwt.generate({ id: data._id }); | ||
res.json({ | ||
status: true, | ||
login: true, | ||
toke: token, | ||
valid: true, | ||
}); | ||
} else { | ||
res.json({ | ||
status: false, | ||
toke: null, | ||
valid: true, | ||
}); | ||
} | ||
} else { | ||
res.json({ | ||
status: false, | ||
toke: null, | ||
valid: false, | ||
}); | ||
} | ||
}) | ||
.catch((error) => res.json(error)); | ||
}) | ||
.catch((error) => { | ||
res.json({ error: error, status: false, token: null }); | ||
}); | ||
} catch (error) { | ||
res.json(error); | ||
} | ||
}, | ||
createUser: async (req, res) => { | ||
console.log("-> create user route"); | ||
const errors = validationResult(req); | ||
console.log(errors); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array(), success: false }); | ||
} | ||
try { | ||
// verify phone number is okay | ||
userAuth | ||
.createUser(req.body.user) | ||
.then(async (response) => { | ||
const token = await jwt.generate({ id: response._id }); | ||
if (response.status) { | ||
res.json({ | ||
status: true, | ||
login: true, | ||
toke: token, | ||
}); | ||
} else { | ||
res.json({ | ||
status: false, | ||
login: false, | ||
toke: null, | ||
}); | ||
} | ||
}) | ||
.catch((data) => { | ||
res.json(data); | ||
}); | ||
} catch (error) { | ||
res.json(error); | ||
} | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const db = require("../../config/db.config"); | ||
const collections = require("../../config/collections.config"); | ||
module.exports = { | ||
checkUser: check, | ||
createUser: (user) => { | ||
user.phone = parseInt(user.phone); | ||
return new Promise((resolve, reject) => { | ||
check(user.phone) | ||
.then((data) => { | ||
if (!data.status) { | ||
db.get() | ||
.collection(collections.USER_COLLECTION) | ||
.insertOne(user) | ||
.then((data) => { | ||
resolve({ status: true, id: data.insertedId }); | ||
}); | ||
} else { | ||
reject({ | ||
status: false, | ||
statusCode: 400, | ||
error: "user already exists", | ||
}); | ||
} | ||
}) | ||
.catch((error) => { | ||
reject({ | ||
status: false, | ||
statusCode: 400, | ||
error: error, | ||
}); | ||
}); | ||
}); | ||
}, | ||
}; | ||
|
||
function check(phone) { | ||
return new Promise((resolve, reject) => { | ||
db.get() | ||
.collection(collections.USER_COLLECTION) | ||
.findOne({ phone: phone }) | ||
.then((data) => { | ||
if (data) { | ||
resolve({ status: true, user: data, statusCode: 200 }); | ||
} else { | ||
resolve({ | ||
status: false, | ||
user: null, | ||
statusCode: 200, | ||
}); | ||
} | ||
}) | ||
.catch((error) => { | ||
reject({ status: false, error: error, statusCode: 500, user: null }); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { body, checkSchema } = require("express-validator"); | ||
|
||
exports.sendOtpValidation = [ | ||
body("phone") | ||
.not() | ||
.isEmpty() | ||
.withMessage("Phone number is required") | ||
.isLength({ min: 10, max: 10 }) | ||
.withMessage("invalid phone number"), | ||
]; | ||
|
||
exports.verifyOtpValidation = [ | ||
body("phone") | ||
.not() | ||
.isEmpty() | ||
.withMessage("Phone number is required") | ||
.isLength({ min: 10, max: 10 }) | ||
.withMessage("invalid phone number"), | ||
|
||
body("otp") | ||
.not() | ||
.isEmpty() | ||
.withMessage("please provide otp") | ||
.isLength({ min: 6, max: 6 }) | ||
.withMessage("invalid otp, otp must be 6 numbers"), | ||
]; | ||
|
||
exports.createUserValidation = [ | ||
body("user.phone") | ||
.not() | ||
.isEmpty() | ||
.withMessage("Phone number is required") | ||
.isLength({ min: 10, max: 10 }) | ||
.withMessage("invalid phone number") | ||
.isNumeric() | ||
.withMessage("phone no must be numerical value"), | ||
body("user.email").isEmail().withMessage("invalid email address"), | ||
body("user.name").not().isEmpty().withMessage("name is required"), | ||
]; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.