Skip to content

Commit

Permalink
Pushing the file
Browse files Browse the repository at this point in the history
  • Loading branch information
rajRishi22 committed Jul 17, 2024
1 parent b29d925 commit 3284154
Show file tree
Hide file tree
Showing 25 changed files with 2,501 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
node_modules
/.pnp
.pnp.js

Expand Down
69 changes: 69 additions & 0 deletions backend/Routes/CreateUser.js
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;
13 changes: 13 additions & 0 deletions backend/Routes/DisplayData.js
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;
22 changes: 22 additions & 0 deletions backend/db.js
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;
23 changes: 23 additions & 0 deletions backend/index.js
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");
});
28 changes: 28 additions & 0 deletions backend/models/User.js
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);
Loading

0 comments on commit 3284154

Please sign in to comment.