spee.ch/helpers/authHelpers.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

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');
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 } })
.then(user => {
2017-11-30 23:46:32 +01:00
return module.exports.returnUserAndChannelInfo(user);
})
2017-11-30 23:46:32 +01:00
.then((userInfo) => {
return done(null, userInfo);
})
.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);
});
});
},
};