Last active
July 9, 2019 13:05
-
-
Save Prashant446/e86205cce7e290375b286a36ac78ce5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const bodyParser=require('body-parser'); | |
const nodemailer = require('nodemailer'); | |
app.use(bodyParser.json()); | |
var firebase = require('firebase'); | |
var validator = require("email-validator"); | |
// const bcrypt = require('bcrypt'); | |
const crypto = require('crypto'); | |
// const saltRounds = 10; | |
var config = { | |
apiKey: "AIzaSyD_BZoGyedQ_MEeOUq4rWJ6MbRPpLveb9E", | |
authDomain: "reactnative-e1c19.firebaseapp.com", | |
databaseURL: "https://reactnative-e1c19.firebaseio.com", | |
projectId: "reactnative-e1c19", | |
storageBucket: "reactnative-e1c19.appspot.com", | |
messagingSenderId: "108896196107", | |
appId: "1:108896196107:web:bbe8f210a885d8f7" | |
}; | |
const port = 3000; | |
firebase.initializeApp(config); | |
// var ip = '0.0.0.0'; | |
app.listen(port, () => console.log(`App listening on port ${port}!`)); | |
//---------------functions---------------------------------------------------------- | |
function encrypt(data, pass) { | |
var cipher = crypto.createCipher('aes-256-ecb', pass); | |
return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); | |
}; | |
function conversion(date){ | |
for(var i = 0; i < date.length; i++){ | |
if(date[i] == '/'){ | |
date = date.substr(0 , i) + '-' + date.substr(i + 1); | |
} | |
}; | |
return date; | |
}; | |
function makeid(length) { | |
var result = ''; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < length; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
//----------------get- Requests----------------------------------------------------------- | |
app.get("/verifymail",function(req,res){ | |
data={ | |
email : req.query.id3, | |
password : req.query.id1, | |
phoneno : req.query.id4 | |
} | |
let paas = (encrypt(data.password, "donate")).toString() ; | |
// req.query.id1 = encrypt(req.query.id, "donate").toString(); | |
if(req.query.id2==paas){ | |
var uid = makeid(16); | |
var date = new Date(Date.now()).toLocaleString(); | |
date = conversion(date); | |
uid = date + uid; | |
firebase.database().ref('/Users').child(uid).set(data); | |
res.send("Verfication Successful"); | |
} | |
else { | |
firebase.database().ref('/Users').push(data); | |
res.send("Verfication Successful"); | |
res.send("Verfication Unsuccessful"); | |
} | |
}); | |
app.get("/projects", function(req, res){ | |
if(req.query.search != null){ | |
console.log(req.query.search); | |
} | |
var projectsRef = firebase.database().ref('/Projects').orderByChild('timestamp'); | |
projectsRef.once('value', (snapshot) => { | |
let objKey = Object.keys(snapshot.val()); | |
let projects = []; | |
for(var i =0 ;i < objKey.length; i++ ){ | |
if(snapshot.val()[objKey[i]].status=="YES"){ | |
// console.log(snapshot.val()[objKey[i]]); | |
projects.push(snapshot.val()[objKey[i]]); | |
} | |
}; | |
res.send(projects); | |
}); | |
}) | |
app.get("/ownprojects", function(req, res){ | |
// if(req.query.email != null){ | |
// console.log(req.query.email); | |
// } | |
var projectsRef = firebase.database().ref('/Projects').orderByChild('timestamp'); | |
projectsRef.once('value', (snapshot) => { | |
let objKey = Object.keys(snapshot.val()); | |
let projects = []; | |
for(var i =0 ;i < objKey.length; i++ ){ | |
if(snapshot.val()[objKey[i]].email==req.query.email &&snapshot.val()[objKey[i]].status=="YES"){ | |
// console.log(snapshot.val()[objKey[i]]); | |
projects.push(snapshot.val()[objKey[i]]); | |
} | |
}; | |
res.send(projects); | |
}); | |
}) | |
// -----------------POST requests---------------------------------------------------- | |
app.post('/adminProjectRequest',function(req,res){ | |
console.log(hi); | |
database= firebase.database(); | |
var ref = database.ref("Projects"); | |
ref.on("value", gotData, errData); | |
function gotData(data) { | |
let projects = data.val(); | |
console.log(hi); | |
res.send(projects); | |
return; | |
}; | |
function errData(err) { | |
res.send(err); | |
return; | |
}; | |
}); | |
//login request---------------- | |
app.post('/login',function(req,res){ | |
let data = { | |
email : req.body.email, | |
password : req.body.password | |
} | |
data.password = encrypt(data.password, "donate").toString(); | |
database= firebase.database(); | |
var ref = database.ref("Users"); | |
ref.on("value", gotData, errData); | |
function gotData(ddata) { | |
let users = ddata.val(); | |
let keys = Object.keys(users); | |
for(var i=0; i<keys.length; i++){ | |
if(users[keys[i]].email == req.body.email && users[keys[i]].password == data.password ){ | |
if(users[keys[i]].email=="admin@donate.com"){ | |
res.send({val:"admin"}); | |
return; | |
} | |
else | |
{res.send({val:"user"}); | |
return; | |
}; | |
}; | |
} | |
res.send({val:"nouser"}); | |
return; | |
}; | |
function errData(err) { | |
res.send(err); | |
return; | |
}; | |
}); | |
//signup requests----- | |
app.post('/',function(req,res){ | |
var data=req.body; | |
firebase.database().ref('/Projects').push(data); | |
res.send(data); | |
}); | |
app.post('/users',function(req,res){ | |
let data = { | |
password : req.body.password , | |
email : req.body.email, | |
phoneno : req.body.phoneno, | |
}; | |
if(req.body.email==''||req.body.password==''||req.body.phoneno==''||validator.validate(req.body.email)===false){ | |
res.send({ val :"dataincomplete"}); | |
return; | |
} | |
var exist =0; | |
database= firebase.database(); | |
var ref = database.ref("Users"); | |
ref.on("value", gotData, errData); | |
function gotData(ddata) { | |
let users = ddata.val(); | |
let keys = Object.keys(users); | |
for(var i=0; i<keys.length; i++){ | |
// console.log(req.body.email); | |
if(users[keys[i]].email == req.body.email){ | |
exist = 1; | |
return; | |
} | |
}; | |
exist = 0; | |
return; | |
} | |
function errData(err) { | |
res.send(err); | |
return ; | |
}; | |
data.password = (encrypt(req.body.password, "donate")).toString(); | |
eemail = encrypt(data.password, "donate").toString(); | |
if(exist==0) | |
{ | |
link="http://"+req.get('host')+"/verifymail?id1="+data.password+"&id2="+eemail+"&id3="+data.email+"&id4="+data.phoneno; | |
const output = ` | |
<p>You made a sign up request on ${new Date(Date.now()).toLocaleString()}</p> | |
<p>Please Click the link to verify the email </p> | |
<a href="${link}">Click here to verify</a> | |
<p>**This is an automatically generated mail. Please do not reply. For any further queries contact Donate App core team</p> | |
`; | |
let transporter = nodemailer.createTransport({ | |
host: 'smtp.gmail.com', | |
port: 587, | |
secure: false, | |
auth: { | |
user: 'donateappreact@gmail.com', | |
pass: 'reactapp1234' | |
}, | |
tls:{ | |
rejectUnauthorized:false | |
} | |
}); | |
let mailOptions = { | |
from: '"DonateApp" <donateappreact@gmail.com>', | |
to: data.email ,// list of receivers | |
subject: 'Confirmation For Mail', | |
html: output | |
}; | |
// send mail with defined transport object | |
transporter.sendMail(mailOptions, (error, info) => { | |
if (error) { | |
return console.log(error); | |
} | |
}); | |
res.send({val :"emailsent"}); | |
return; | |
}else { | |
res.send({ val :"alreadyemail"}); | |
} | |
// }) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment