Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3eb8037
Add register and login end-points
Arpitkr Jul 20, 2021
c05dcab
Merge branch 'main' of https://github.com/Arpitkr/Assignment-1 into main
Arpitkr Jul 20, 2021
a876c25
Add main.js
Arpitkr Jul 27, 2021
33f38a6
Add middleware to check presence and validity of JWT
Arpitkr Jul 27, 2021
e6d336c
Add code for end point to add notes
Arpitkr Jul 27, 2021
9adf393
Add code for end point to delete notes
Arpitkr Jul 27, 2021
a027a61
Add code for end point to register new user
Arpitkr Jul 27, 2021
bc64140
Add code for end point to login existing user
Arpitkr Jul 27, 2021
88df553
Add code for end point to view note
Arpitkr Jul 27, 2021
0773f24
Add dependencies
Arpitkr Jul 27, 2021
c50f12d
Modify request method for /view to POST
Arpitkr Jul 30, 2021
60a0d08
Modify addNote end point to link with front end
Arpitkr Jul 30, 2021
fdca85e
Modify delete end point to link with front end
Arpitkr Jul 30, 2021
1047dc0
Modify login end point to link with front end
Arpitkr Jul 30, 2021
08c434c
Modify register end point to link with front end
Arpitkr Jul 30, 2021
dd165a5
Modify view end point to link with front end
Arpitkr Jul 30, 2021
7aeedea
Modify verify middleware to link with front end
Arpitkr Jul 30, 2021
86a6832
Add dependencies
Arpitkr Jul 30, 2021
c89d451
Add code for front-end in react.js
Arpitkr Jul 30, 2021
ea99b20
Add README
Arpitkr Jul 30, 2021
adf7a8a
Merge branch 'Web-Division-IITK:main' into main
Arpitkr Jul 30, 2021
0dbfceb
Connect to MongoDB ATLAS
Arpitkr Aug 1, 2021
ee5da6c
Modify frontend
Arpitkr Aug 1, 2021
ff59ee5
Modify package.json
Arpitkr Aug 1, 2021
08b215f
Add .gitignore
Arpitkr Aug 1, 2021
d332b78
Modify backend
Arpitkr Aug 2, 2021
8f6f26b
Modify front end
Arpitkr Aug 2, 2021
09a72ae
Add package.json
Arpitkr Aug 2, 2021
2137a27
Add readme
Arpitkr Aug 2, 2021
b5158c8
Merge branch 'main' of https://github.com/Arpitkr/Assignment-1 into main
Arpitkr Aug 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Assignment1/node_modules
24 changes: 24 additions & 0 deletions Assignment1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This is a code base for a Note-making app created using express for back-end, react for front-end and mongodb for data storage.

# Back-end

1. The code is organised into two directories viz backend and frontend.

2. /backend contains the entry point file viz main.js.

3. /backend/routeHandler contains the handler functions for register, login, add notes, delete notes and view notes end points.

4. /backend/middleware contains middleware for verifying user identity before accessing /view, /addNotes and /delete end points.

5. User authentication and authorisation is achieved using JSON web tokens. Feature for refreshing tokens is still to be added.

6. MongoDB is used as a database for storing user information.

# Front-end

1. /frontend/src/App.js is entry point for the app using various react components defined in frontend/src/Components directory.

2. /frontend/src/Components contains the various react components.

# Access the app on https://immense-inlet-55002.herokuapp.com/

3 changes: 3 additions & 0 deletions Assignment1/backend/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"secret" : "Mysecret"
}
38 changes: 38 additions & 0 deletions Assignment1/backend/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const app = express();
const cors = require('cors');
const verify = require('./middleware/verify');
const register = require('./routeHandler/register');
const view = require('./routeHandler/view')
const add = require('./routeHandler/addNote')
const login = require('./routeHandler/login');
const deleteNote = require('./routeHandler/delete');
const path = require('path');
app.use(express.json());
app.use(cors());

// if(process.env.NODE_ENV === 'production'){
// app.use(express.static('../frontend/build'));
// app.post('*',(req,res)=>{
// res.sendFile(path.resolve(dirname,'build','index.html'));
// })
app.use(express.static(path.join(__dirname, '../frontend/build')));
// app.use('/', (req, res) => {
// res.sendFile(path.join(__dirname, '../frontend/build'));
// })
// }


const port = process.env.PORT || 5000;

app.post('/login',login)
app.post("/register",register);
app.use(verify);
app.post("/view",view.view);
app.post('/addNotes',add);
app.post('/delete',deleteNote);
// app.post("/login",auth.login);

app.listen(port,()=>console.log("Server started on",port));


33 changes: 33 additions & 0 deletions Assignment1/backend/middleware/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const jwt = require('jsonwebtoken');
const config = require('../config.json');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})
const checkEmail = require('../routeHandler/view').checkEmail;

async function verify(req,res,next){
let dbo = db.db('User');
let result = await checkEmail(req.body.email,dbo,res);
if(!result.check){
return;
}
if (!req.headers['authorization']){
return res.status(401).json({"auth":false,"msg": "Status unauthorised"});
}
let token = req.headers['authorization'].split(' ')[1];
if (!token){
return res.status(401).json({"auth":false,"msg": "Status unauthorised"});
}
try{
let payload = jwt.verify(token,config.secret);
}catch(err){
return res.status(401).json({"auth":false,"msg": "Status unauthorised."});
}
next();
}

module.exports = verify;
28 changes: 28 additions & 0 deletions Assignment1/backend/routeHandler/addNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})

async function addNotes(req,res){
let dbo = db.db("User");
let result = await dbo.collection("Customers").find({email : req.body.email}).toArray();

if(result[0].notes){
result[0].notes.unshift(req.body.data);
await dbo.collection("Customers").updateOne({email : req.body.email},{$set : {notes : result[0].notes}});
let index = parseInt(req.body.index);
if(!(index===-1)){
result[0].notes.splice(index+1,1);
await dbo.collection("Customers").updateOne({email : req.body.email},{$set : {notes : result[0].notes}});
}
}
else{
await dbo.collection("Customers").updateOne({email : req.body.email},{$set : {notes : new Array(req.body.data)}});
}
return res.status(200).json({"msg" : "Notes added successfully","data":result[0].notes});
}

module.exports = addNotes;
22 changes: 22 additions & 0 deletions Assignment1/backend/routeHandler/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var MongoClient = require('mongodb').MongoClient;
// var url = "mongodb://localhost:27017/";
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})

async function deleteNote(req,res){
let dbo = db.db("User");
let result = await dbo.collection("Customers").find({email: req.body.email}).toArray();
let index = parseInt(req.body.index);
if(result[0].notes[index]){
result[0].notes.splice(index,1);
await dbo.collection("Customers").updateOne({email : req.body.email},{$set : {notes : result[0].notes}});
res.status(200).json({"data":result[0].notes,"msg":"Deleted successfully"});
}
return res.status(400);
}

module.exports = deleteNote;
26 changes: 26 additions & 0 deletions Assignment1/backend/routeHandler/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const bcrypt = require('bcryptjs');
const config = require('../config.json')
const checkEmail = require('./view').checkEmail;

var MongoClient = require('mongodb').MongoClient;
const jwt = require('jsonwebtoken');
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})

async function login(req,res){
let dbo = db.db("User");
let result = await checkEmail(req.body.email,dbo,res);
if(result.check){
if(!bcrypt.compareSync(req.body.password,result.result[0].password)){
return res.status(401).json({"msg":"Invalid password"});
}
let token = jwt.sign({email: req.body.email},config.secret,{expiresIn : 86400});
return res.status(200).json({"auth" :true,"msg":"User logged in successfully","token":token});
}
}

module.exports = login;
30 changes: 30 additions & 0 deletions Assignment1/backend/routeHandler/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const bcrypt = require('bcryptjs');
const config = require('../config.json')

var MongoClient = require('mongodb').MongoClient;
const jwt = require('jsonwebtoken');
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})


async function register(req,res){
let dbo = db.db("User");
let query = {email : req.body.email};
let result = await dbo.collection("Customers").find(query).toArray();
res.set('Access-Control-Allow-Origin', '*');
if(result.length>0){
return res.status(401).json({"msg": "Email already in use"});
}
let hashedPassword = bcrypt.hashSync(req.body.password,8);
req.body.password = hashedPassword;
let data = {email : req.body.email, password : req.body.password}
await dbo.collection("Customers").insertOne(data);
let token = jwt.sign({email: req.body.email},config.secret,{expiresIn : 86400});
return res.status(200).json({"auth" :true,"msg":"User added successfully",token:token});
}

module.exports = register;
27 changes: 27 additions & 0 deletions Assignment1/backend/routeHandler/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb+srv://Arpitkr:Arpit299792458@cluster0.z2ut1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
let db = null;
MongoClient.connect(url,function(err,client){
if(err) throw err
db = client
})

async function checkEmail(email,dbo,res){
let result = await dbo.collection("Customers").find({email : email}).toArray();
if(result.length == 0){
res.status(400).json({"msg" : `${email} is not registered.`});
return {check : false, result : result};
}
return {check : true, result : result};
}


//This end point receives a post request containing the email and noteID of user and returns the corresponding note in json format
async function view(req,res){
let dbo = db.db("User");
let result = await dbo.collection("Customers").find({email : req.body.email}).toArray();

res.status(200).json({"data" : result[0].notes});
}

module.exports = {view,checkEmail};
23 changes: 23 additions & 0 deletions Assignment1/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions Assignment1/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Loading