-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b29d925
commit 3284154
Showing
25 changed files
with
2,501 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const express = require("express"); | ||
const User = require("../models/User"); | ||
const { body, validationResult } = require("express-validator"); | ||
|
||
const bcrpyt = require("bcryptjs"); | ||
const jwt = require("jsonwebtoken"); | ||
const router = express.Router(); | ||
const jwtsecret="MynameisKiyotakaAyanokoji"; //secret key for jwt token | ||
router.post( | ||
"/createUser", | ||
[body("email").isEmail(), body("password","Incorrect Password").isLength({ min: 5 })], | ||
async (req, res) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
|
||
const salt=await bcrpyt.genSalt(10); //generating salt | ||
let secPassword = await bcrpyt.hash(req.body.password,salt); //parameter to be hashed, salt | ||
|
||
|
||
try { | ||
await User.create({ | ||
name: req.body.name, | ||
location: req.body.location, | ||
email: req.body.email, | ||
password: secPassword, | ||
}); | ||
res.json({ success: true }); | ||
} catch (err) { | ||
console.log(err); | ||
res.json({ success: false }); | ||
} | ||
} | ||
); | ||
|
||
router.post( | ||
"/loginuser", | ||
[body("email").isEmail(), body("password").isLength({ min: 5 })], | ||
async (req, res) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
let email = req.body.email; | ||
try { | ||
let userData = await User.findOne({ email: email }); | ||
if (!userData) { | ||
return res.status(400).json({ errors: "Try logging in again" }); | ||
} | ||
const pwdCompare=await bcrpyt.compare(req.body.password,userData.password); | ||
if (!pwdCompare) { | ||
return res.status(400).json({ errors: "Try logging in again" }); | ||
} | ||
const data={ | ||
user:{ | ||
id:userData.id | ||
} | ||
} | ||
const authToken=jwt.sign(data,jwtsecret); | ||
return res.json({ success: true ,authToken:authToken}); | ||
} catch (err) { | ||
console.log(err); | ||
res.json({ success: false }); | ||
} | ||
} | ||
); | ||
|
||
module.exports = router; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const express = require('express'); | ||
const router=express.Router(); | ||
router.post('/foodData',async(req,res)=>{ | ||
try{ | ||
console.log(global.foodData2); | ||
res.send([global.foodData2,global.foodCategory]); | ||
}catch(err){ | ||
console.log(err); | ||
res.send('Server Error') | ||
} | ||
}); | ||
|
||
module.exports=router; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoURI = 'mongodb+srv://RishiRaj:pass%[email protected]/zaykadata?retryWrites=true&w=majority&appName=Cluster0'; | ||
|
||
const mongoDB = async () => { | ||
try { | ||
await mongoose.connect(mongoURI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, // added for better connection handling | ||
}); | ||
console.log("Connected to MongoDB"); | ||
|
||
const fetched_data = await mongoose.connection.db.collection("foodData2").find({}).toArray(); | ||
global.foodData2 = fetched_data; | ||
const foodCategory = await mongoose.connection.db.collection("foodCategory").find({}).toArray(); | ||
global.foodCategory = foodCategory; | ||
console.log("Fetched data from MongoDB"); | ||
} catch (err) { | ||
console.log("Error connecting to MongoDB:", err); | ||
} | ||
}; | ||
|
||
module.exports = mongoDB; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const express=require('express') | ||
const app=express() | ||
const port=5000 | ||
const mongoDB=require("./db"); | ||
const cors = require('cors'); | ||
app.use(cors()); | ||
mongoDB(); | ||
app.get('/',(req,res)=>{ | ||
res.send('Hello world'); | ||
}) | ||
app.use((req,res,next)=>{ | ||
res.setHeader("Acess-Control-Allow-Origin","http://localhost:3000"); | ||
res.header("Acess-Control-Allow-Headers", | ||
"Origin, X-Requested-With, Conten-Type, Accept" | ||
); | ||
next(); | ||
}); | ||
app.use(express.json()); | ||
app.use('/api',require('./Routes/CreateUser')); | ||
app.use('/api',require('./Routes/DisplayData')); | ||
app.listen(port,()=>{ | ||
console.log("App listening on port 5000"); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mongoose=require('mongoose'); | ||
|
||
const {Schema} = mongoose; | ||
|
||
const UserSchema = new Schema({ | ||
name:{ | ||
type:String, | ||
required:true | ||
}, | ||
location:{ | ||
type:String, | ||
required:true | ||
}, | ||
email:{ | ||
type:String, | ||
required:true | ||
}, | ||
password:{ | ||
type:String, | ||
required:true | ||
}, | ||
date:{ | ||
type:Date, | ||
default:Date.now | ||
} | ||
}) | ||
|
||
module.exports=mongoose.model('user',UserSchema); |
Oops, something went wrong.