diff --git a/helpers/authHelpers.js b/helpers/authHelpers.js index dc2f5548..e9bf0210 100644 --- a/helpers/authHelpers.js +++ b/helpers/authHelpers.js @@ -1,9 +1,10 @@ -const db = require('../models'); // require our models for syncing +// const db = require('../models'); // require our models for syncing const logger = require('winston'); module.exports = { populateLocalsDotUser (req, res, next) { if (req.user) { + logger.debug('populating res.locals.user'); res.locals.user = { id : req.user.id, userName : req.user.userName, @@ -14,42 +15,12 @@ module.exports = { } next(); }, - serializeSpeechUser (user, done) { // returns user id to be serialized into session token - logger.debug('serializing session'); - done(null, user.id); + serializeSpeechUser (user, done) { // returns user data to be serialized into session + logger.debug('serializing user'); + done(null, user); }, - deserializeSpeechUser (id, done) { // deserializes session token and provides user from user id - logger.debug('deserializing session'); - return db.User.findOne({ where: { id } }) - .then(user => { - return module.exports.returnUserAndChannelInfo(user); - }) - .then((userInfo) => { - return done(null, userInfo); - }) - .catch(error => { - return done(error); - }); - }, - returnUserAndChannelInfo (userInstance) { - return new Promise((resolve, reject) => { - let userInfo = {}; - userInfo['id'] = userInstance.id; - userInfo['userName'] = userInstance.userName; - userInstance - .getChannel() - .then(({channelName, channelClaimId}) => { - userInfo['channelName'] = channelName; - userInfo['channelClaimId'] = channelClaimId; - return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName); - }) - .then(shortChannelId => { - userInfo['shortChannelId'] = shortChannelId; - resolve(userInfo); - }) - .catch(error => { - reject(error); - }); - }); + deserializeSpeechUser (user, done) { // deserializes session and populates additional info to req.user + logger.debug('deserializing user'); + done(null, user); }, }; diff --git a/passport/local-login.js b/passport/local-login.js index 5910d96a..e3d72e88 100644 --- a/passport/local-login.js +++ b/passport/local-login.js @@ -1,14 +1,33 @@ - const PassportLocalStrategy = require('passport-local').Strategy; const db = require('../models'); const logger = require('winston'); -const { returnUserAndChannelInfo } = require('../helpers/authHelpers.js'); + +function returnUserAndChannelInfo (userInstance) { + return new Promise((resolve, reject) => { + let userInfo = {}; + userInfo['id'] = userInstance.id; + userInfo['userName'] = userInstance.userName; + userInstance + .getChannel() + .then(({channelName, channelClaimId}) => { + userInfo['channelName'] = channelName; + userInfo['channelClaimId'] = channelClaimId; + return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName); + }) + .then(shortChannelId => { + userInfo['shortChannelId'] = shortChannelId; + resolve(userInfo); + }) + .catch(error => { + reject(error); + }); + }); +} module.exports = new PassportLocalStrategy( { usernameField: 'username', passwordField: 'password', - // session : false, }, (username, password, done) => { logger.debug('logging user in'); @@ -20,7 +39,7 @@ module.exports = new PassportLocalStrategy( // logger.debug('no user found'); return done(null, false, {message: 'Incorrect username or password.'}); } - return user.comparePassword(password, (passwordErr, isMatch) => { + user.comparePassword(password, (passwordErr, isMatch) => { if (passwordErr) { logger.error('passwordErr:', passwordErr); return done(null, false, {message: passwordErr}); diff --git a/passport/local-signup.js b/passport/local-signup.js index def287a1..83639248 100644 --- a/passport/local-signup.js +++ b/passport/local-signup.js @@ -7,7 +7,6 @@ module.exports = new PassportLocalStrategy( { usernameField: 'username', passwordField: 'password', - // session: false, // set to false because we will use token approach to auth }, (username, password, done) => { logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);