Skip to content

Commit

Permalink
fixing some production level bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chahakshahcs5 committed Jan 6, 2021
1 parent 676ca62 commit d715b3d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
Empty file added access.log
Empty file.
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.0",
"description": "",
"main": "server.js",
"engine": {
"node": "14.15.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
Expand All @@ -12,6 +15,7 @@
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^2.4.1",
"csurf": "^1.11.0",
Expand All @@ -20,8 +24,10 @@
"express": "^4.17.1",
"express-flash": "0.0.2",
"express-session": "^1.17.1",
"helmet": "^4.3.1",
"mongodb": "^3.6.3",
"mongoose": "^5.11.7",
"morgan": "^1.10.0",
"nodemailer": "^6.4.17",
"nodemailer-sendgrid-transport": "^0.2.0"
}
Expand Down
8 changes: 2 additions & 6 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ router.post("/reset", (req,res) => {
to: email,
from: '[email protected]',
subject: 'Reset Password',
html: `<h1>You requsted for a password reset <a href="http://localhost:3000/reset/${token}">Click here</a> to reset password</h1>`
html: `<h1>You requsted for a password reset <a href="http://my-magic-notes/reset/${token}.herokuapp.com">Click here</a> to reset password</h1>`
})
})
.catch(err => {
Expand All @@ -125,7 +125,6 @@ router.post("/update-password", (req,res) => {
const newPassword = req.body.newPassword;
const confirmPassword = req.body.confirmPassword;
let resetUser;
console.log(token);
if(newPassword == confirmPassword){
User.findOne({
resetToken: token, resetTokenExpiration: {$gt: Date.now()}
Expand Down Expand Up @@ -153,7 +152,6 @@ router.post("/update-password", (req,res) => {
} else {
res.redirect(`/reset/${token}`)
req.flash('data', 'password does not match')
console.log(token);
}

});
Expand All @@ -166,7 +164,6 @@ router.get('/reset/:token', (req,res) => {
message = null;
}
const token = req.params.token;
console.log(token);
User.findOne({
resetToken: token, resetTokenExpiration: {$gt: Date.now()}
})
Expand Down Expand Up @@ -218,7 +215,7 @@ router.post("/add-user", (req, res) => {
to: email,
from: '[email protected]',
subject: 'Signup',
html: `<h1>You Successfully Signed Up! <a href="http://localhost:3000/login">Click here</a> to log in</h1>`
html: `<h1>You Successfully Signed Up! <a href="http://my-magic-notes/login.herokuapp.com">Click here</a> to log in</h1>`
})
.catch(err => {
console.log(err);
Expand Down Expand Up @@ -251,7 +248,6 @@ router.post("/user-login", (req, res) => {
.then((doMatch) => {
if (doMatch) {
const user_id = user._id;
console.log(user);
req.session.isLoggedin = true;
res.redirect(`home/${user_id}`);
} else {
Expand Down
23 changes: 22 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const indexRouter = require('./routes/index');
const userRouter = require('./routes/user');

const fs = require('fs');

const path = require('path');

const dotenv = require('dotenv');

dotenv.config({path: './config/config.env'});
Expand All @@ -15,12 +19,18 @@ const session = require('express-session');

const MongoDBStore = require('connect-mongodb-session')(session);

const helmet = require('helmet');

const compression = require('compression');

const morgan = require('morgan');

const csrf = require('csurf');

const flash = require('connect-flash');

const store = new MongoDBStore({
uri: 'mongodb+srv://rajshahcs5:@[email protected]/magicnote?retryWrites=true&w=majority',
uri: process.env.MONGO_URI,
collection: 'sessions'
});

Expand All @@ -38,13 +48,24 @@ app.use(session({secret: 'chahak', store: store, resave: true, saveUninitialized

const csrfProtection = csrf();

const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'access.log'),
{ flags: 'a'}
);

app.use(csrfProtection);

app.use((req,res,next) => {
res.locals.csrfToken = req.csrfToken();
next();
});

app.use(helmet());

app.use(compression());

app.use(morgan('combined', { stream: accessLogStream}));

app.use(flash());

mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true, useUnifiedTopology: true})
Expand Down

0 comments on commit d715b3d

Please sign in to comment.