Skip to content

Commit

Permalink
Merge pull request #2 from aninarafath6/backend
Browse files Browse the repository at this point in the history
Backend
  • Loading branch information
aninarafath6 committed Aug 26, 2022
2 parents fb16bcb + b792d32 commit e0a5d3e
Show file tree
Hide file tree
Showing 4,534 changed files with 799,433 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
3 changes: 3 additions & 0 deletions arnhss-backend/config/collections.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
USER_COLLECTION: "users",
};
17 changes: 17 additions & 0 deletions arnhss-backend/config/db.config.js
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;
};
117 changes: 117 additions & 0 deletions arnhss-backend/controller/user.controller.js
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);
}
},
};
17 changes: 17 additions & 0 deletions arnhss-backend/dist/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions arnhss-backend/dist/server.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions arnhss-backend/helpers/auth/user.auth.js
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 });
});
});
}
39 changes: 39 additions & 0 deletions arnhss-backend/middleware/auth.middleware.js
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"),
];
Empty file.
1 change: 1 addition & 0 deletions arnhss-backend/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions arnhss-backend/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions arnhss-backend/node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions arnhss-backend/node_modules/.bin/nopt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions arnhss-backend/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0a5d3e

Please sign in to comment.