Skip to content

Commit

Permalink
Merge pull request #2 from Deepak-Sangle/web-client
Browse files Browse the repository at this point in the history
Web client
  • Loading branch information
Deepak-Sangle authored Sep 20, 2023
2 parents a26091b + 164a0f1 commit 760ca43
Show file tree
Hide file tree
Showing 30 changed files with 33,077 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
40 changes: 40 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
.DS_Store

.env
File renamed without changes.
40 changes: 40 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require("dotenv").config();
const express = require('express');
const cors = require('cors');
const app = express();
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const PORT = process.env.PORT || 5000;

const corsOptions = {
origin: ['http://localhost:3000', 'https://dear-diary-api.onrender.com'],
credentials: true,
methods: ['GET', 'POST']
};

app.use(cors(corsOptions));
app.use(cookieParser());

//Set up MongoDB Database
mongoose.connect(process.env.MONGOURI);

mongoose.connection.on('connected',()=>{
console.log("Database connection On");
});
mongoose.connection.on('error',(err)=>{
console.log("Error Connecting: ", err);
});

//Middleware Functions
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

//Route Requests
app.use(require('./routes/auth'));
app.use(require('./routes/event'));

//Listen Port
app.listen(PORT, (req,res)=>{
console.log(`Listening to the port ${PORT}`);
});
38 changes: 38 additions & 0 deletions server/helper/cryptography.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const crypto = require('crypto');

const NUM_BYTES = 32;
const algorithm = "aes-256-cbc";

function encryptData(message, initVector, key) {
const cipher = crypto.createCipheriv(algorithm, key, initVector);
let encryptedData = cipher.update(message, "utf-8", "hex");
encryptedData += cipher.final("hex");
return encryptedData;
}

function decryptData(encryptedData, initVector, key){
const decipher = crypto.createDecipheriv(algorithm, key, initVector);
let decryptedData = decipher.update(encryptedData, "hex", "utf-8");
decryptedData += decipher.final("utf-8");
return decryptedData;
}

function getMasterInitVector(){
const ivHex = process.env.MASTER_INIT_VECTOR;
const initVector = Buffer.from(ivHex, 'hex');
return initVector;
}

function getMasterSecretKey(){
const secretHex = process.env.MASTER_SECRET_KEY;
const secretKey = Buffer.from(secretHex, 'hex');
return secretKey;
}

module.exports = {
encryptData,
decryptData,
getMasterSecretKey,
getMasterInitVector,
NUM_BYTES
}
21 changes: 21 additions & 0 deletions server/middleware/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const User = require('../models/user');
const jwt = require("jsonwebtoken");

async function isTokenValid(req, res, next){
try {
const token = req.cookies.token;
if (!token) return res.status(401).send({message : "Cookies not present for authorization", success : false});
const verified = jwt.verify(token, process.env.JWT_SECRET);
if (!verified) return res.status(401).send({message : "Invalid JWT Token for authorization", success : false});
const user = await User.findById({_id : verified.id});
if (!user) return res.status(401).send({message : "User with given JWT Payload does not exists", success : false});
req.id = verified.id;
return next();
} catch (err) {
return res.status(500).json({error: err.message, success : false });
}
}

module.exports = {
isTokenValid
}
23 changes: 23 additions & 0 deletions server/models/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');

const eventSchema = new mongoose.Schema({
createdBy : {
type : mongoose.Schema.Types.ObjectId,
required : true
},
createdAt : {
type : Date,
required : true
},
detail : {
type : String
},
initVector : {
type : String,
required : true
}
});

const Event = mongoose.model("Event", eventSchema);

module.exports = Event;
26 changes: 26 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
name : {
type: String,
required : true
},
password : {
type : String,
required : true
},
lastEntry : {
type : Date,
},
DP : {
type : String,
},
secretKey : {
type : String,
required : true
}
});

const User = mongoose.model("User", userSchema);

module.exports = User;
Loading

0 comments on commit 760ca43

Please sign in to comment.