changed passport session strategy to store user info

This commit is contained in:
bill bittner 2017-11-30 17:24:48 -08:00
parent 7b7627ab27
commit c509c35926
3 changed files with 31 additions and 42 deletions

View file

@ -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);
},
};

View file

@ -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});

View file

@ -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} .`);