2017-10-11 01:00:08 +02:00
|
|
|
const db = require('../models'); // require our models for syncing
|
|
|
|
const logger = require('winston');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
populateLocalsDotUser (req, res, next) {
|
|
|
|
if (req.user) {
|
|
|
|
res.locals.user = {
|
|
|
|
id : req.user.id,
|
|
|
|
userName : req.user.userName,
|
|
|
|
channelName : req.user.channelName,
|
|
|
|
channelClaimId: req.user.channelClaimId,
|
|
|
|
shortChannelId: req.user.shortChannelId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
2017-11-30 23:46:32 +01:00
|
|
|
serializeSpeechUser (user, done) { // returns user id to be serialized into session token
|
|
|
|
logger.debug('serializing session');
|
2017-10-11 01:00:08 +02:00
|
|
|
done(null, user.id);
|
|
|
|
},
|
2017-11-30 23:46:32 +01:00
|
|
|
deserializeSpeechUser (id, done) { // deserializes session token and provides user from user id
|
|
|
|
logger.debug('deserializing session');
|
|
|
|
return db.User.findOne({ where: { id } })
|
2017-10-11 01:00:08 +02:00
|
|
|
.then(user => {
|
2017-11-30 23:46:32 +01:00
|
|
|
return module.exports.returnUserAndChannelInfo(user);
|
2017-10-11 01:00:08 +02:00
|
|
|
})
|
2017-11-30 23:46:32 +01:00
|
|
|
.then((userInfo) => {
|
|
|
|
return done(null, userInfo);
|
2017-10-11 01:00:08 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-11-30 23:46:32 +01:00
|
|
|
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);
|
|
|
|
});
|
2017-10-11 01:00:08 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|