Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Add Flow Question APIs #305

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# OSX
#
.DS_Store

backend/app-env
# Xcode
#
build/
Expand Down Expand Up @@ -51,3 +51,11 @@ buck-out/
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots


#app-env for backend
backend/app-env

#React NPM Modules
client/node_modules
client/app-env
19 changes: 19 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Steps for running the backend server

1. Clone the repositiory.
2. Go to the backend folder.
3. run `npm install` to install all the packages dependencies for the project.
4. Create a free account and sandbox at MLab or Mongo atlas then create a user (After this you will have URL with username and password)
5. create a app-env file.
6. Add `EXPORT MONGOURL="XXXXX"` <br/>
`EXPORT USERNAME="XXXXX"` <br/>
`EXPORT PASSWORD="XXXXX"`

7. run the `source app-env` to add all the enviroment variables to the system.
8. Go to `config\DBSetup.js` to change<br>
`DBURL = process.env.MONGOURL`
9. Add `USERNAME=process.env.USERNAME`<br/>
`PASSWORD=process.env.PASSWORD`
10. Now run `npm run dev`
11. Server will be running at port `3000` or any free port.

91 changes: 91 additions & 0 deletions backend/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../server');
var debug = require('debug')('backend');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '4500');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
console.log("Server listening at " + addr.port);
}
4 changes: 4 additions & 0 deletions backend/config/DBSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
DBURL:"mongodb://ds261136.mlab.com:61136/denguestop",
secret: "SecretToken"
};
163 changes: 163 additions & 0 deletions backend/controllers/Authorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const express = require("express");
const router = express.Router();
const bodyparser = require("body-parser");
const bcrypt = require("bcryptjs");
const passport = require("passport");
const passportjwt = require("passport-jwt");
const jwt = require("jsonwebtoken");
const key = require("../config/DBSetup").secret;
const mime=require("mime");
const path = require('path');
var fs = require('fs');
const multer = require('multer');
const zip=require("express-zip");

router.use(bodyparser.urlencoded({ extended: false }));
router.use(bodyparser.json());

const User = require("../models/User");

exports.Landing=(req,res)=>{
res.send("<p>Landing Page - Dengue Stop");
}

exports.register = (req, res) => {
User.findOne({
$or: [
{ email : req.body.email },
{ phoneNumber: req.body.phoneNumber },
{username:req.body.username}
]
})
.then(user => {
if (user) {
res
.status(200)
.json({ status: "ERROR", message: "User already registered" });
} else {
const newPerson = new User({
username:req.body.username,
email: req.body.email,
phoneNumber: req.body.phoneNumber,
password: req.body.password
});
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newPerson.password, salt, function(err, hash) {
// Store hash in your password DB.
newPerson.password = hash;

newPerson
.save()
.then(person => {
res.status(200).json({ status: "SUCCESS", message: "User successfully registered" });
})
.catch(err => {
console.log(err);
res.send(err)
});

});
});
}
})
.catch(err => {
console.log(err);
res.send(err)
});
};



exports.login=(req, res, next) => {
User.findOne({
$or: [
{ email : req.body.email },
{ phoneNumber: req.body.phoneNumber }
]
})
.then(user => {
if (user) {
if (user.email == req.body.email || user.phoneNumber==req.body.phoneNumber) {
bcrypt
.compare(req.body.password, user.password)
.then(res1 => {
console.log(user.password);
console.log(res1);
if(res1)
{
const payload = {
username: user.username,
phoneNumber: user.phoneNumber,
id: user.id,
email: user.email,
profilepic: user.profilepic
};
jwt.sign(payload, key, (err, token) => {
if (err) throw err;
res.json({ status: "SUCCESS", token: "Bearer " + token ,userId:payload.id});
});
}

else {
res.json({ status: "ERROR", message: "Invalid Credentials"});
}

}).catch(err=>{console.log(err);
res.json({ status: "ERROR", message: "Password check failed - Server Error"});
});
}

else{
res.json({ status: "ERROR", message: "Invalid User"});
}

} else {
res.json({ status: "ERROR", message: "User not found"});
}


})
.catch(err => {
res.json(err);
console.log(err)});
}


exports.updatePassword=(req, res) => {
const phoneNumber=req.body.phoneNumber;
const email=req.body.email;
const newPassword=req.body.newPassword;
if(email==req.user.email || phoneNumber==req.user.phoneNumber )
{

bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newPassword, salt, function(err, hash) {

User.findOneAndUpdate({$or:[{phoneNumber: phoneNumber},{email:email}]},{$set:{password:hash}})
.then(
user=>{
if(user!=null)
{
res.json({status: "SUCCESS", message:"Successfully updated the password"})
}
else{
res.json({status: "ERROR" , message:"Phone No or Email Error"} );
}
}
)
.catch(err=>{
res.json({status: "ERROR"} );
console.log("Update Password Error" + err);
})

})});

}
else{
res.json({status: "ERROR" , message:"Invalid user to update password"} );

}
}



Loading