From ef773b4a44c7374dc373e81b38647f49e581a61d Mon Sep 17 00:00:00 2001 From: jkaplon Date: Fri, 14 Apr 2017 11:42:15 -0400 Subject: [PATCH] Refactor to prep for accessing cookie/session data from within socket.io connections. --- server.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index 5b1bb11..ab81ce0 100644 --- a/server.js +++ b/server.js @@ -1,12 +1,28 @@ -var express = require("express"); -var bodyParser = require("body-parser"); +var express = require('express'); var app = express(); +var bodyParser = require('body-parser'); +app.use(bodyParser.text()); // Use defaults for now, size limit is 100kb. +app.use(bodyParser.urlencoded({ extended: true })); // Also need url encoding to handle login form. var winston = require('winston'); winston.add(winston.transports.File, { filename: './logs/notes.kaplon.us.log', maxsize: 5000000 }); // 5MB var fileSystem = require('fs'); +var notePath = __dirname + '/note-data/allNotes.txt'; +var favicon = require('serve-favicon'); +app.use(favicon(__dirname + '/assets/favicon.ico')); // Put this before setting static dir. +app.use(express.static('assets')); + +/*----------------------------------------- +Ordering of these configs is important, don't shuffle them around. +------------------------------------------*/ var passport = require('passport'); var Strategy = require('passport-local').Strategy; var db = require('./db'); +var cookieParser = require('cookie-parser'); +app.use(cookieParser()); +var sessionStore = new require('express-session').MemoryStore(); +var session = require('express-session')({ secret: 'here kitty kitty', resave: false, saveUninitialized: false, store: sessionStore }); +app.use(session); +//---------------------------- // Configure the local strategy for use by Passport. // The local strategy require a `verify` function which receives the credentials @@ -40,17 +56,6 @@ passport.deserializeUser(function(id, cb) { }); }); -app.set('views', __dirname + '/views'); -var favicon = require('serve-favicon'); -app.use(favicon(__dirname + '/assets/favicon.ico')); // Put this before setting static dir. - -app.use(express.static('assets')); -app.use(require('cookie-parser')()); -app.use(bodyParser.text()); // Use defaults for now, size limit is 100kb. -app.use(bodyParser.urlencoded({ extended: true })); // Also need url encoding to handle login form. -app.use(require('express-session')({ secret: 'here kitty kitty', resave: false, saveUninitialized: false })); -var notePath = __dirname + '/note-data/allNotes.txt'; - // Initialize Passport and restore authentication state, if any, from the session. app.use(passport.initialize()); app.use(passport.session()); @@ -80,7 +85,7 @@ app.get('/login', function(req, res){ app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { - winston.info('sucessful login'); + winston.info('sucessful login for user, ' + req.user.username); res.redirect('/'); });