ohm
Version:
Node.js Session Manager
81 lines (65 loc) • 2.37 kB
JavaScript
'use strict';
(function () {
'use strict';
var _ = require('lodash');
var ViewerContext = include('models/ViewerContext.js');
var fs = require('fs');
var path = require('path');
function BaseController(req, res) {
var _this = this;
this.config = req.app.locals.config;
this.req = req;
this.res = res;
this.viewerContext = null;
if (this.config.manifestPath) {
this.manifest = JSON.parse(fs.readFileSync(this.config.manifestPath, { encoding: 'utf-8' }));
}
return ViewerContext.genFromViewerContextID(this.req.session.viewerContextID, function (err, viewerContext) {
if (viewerContext.getCredential(_this.config.PRIMARY_ACCOUNT_KEY)) {
return ViewerContext.genFromPrimaryAccount(_this.config.PRIMARY_ACCOUNT_KEY, viewerContext.getCredential(_this.config.PRIMARY_ACCOUNT_KEY), _this.setReqSession.bind(_this));
} else {
_this.setReqSession(err, viewerContext);
}
});
}
_.extend(BaseController.prototype, {
setReqSession: function setReqSession(err, viewerContext) {
if (viewerContext) {
this.viewerContext = viewerContext;
if (this.req.session.viewerContextID !== viewerContext.id) {
this.req.session.viewerContextID = viewerContext.id;
}
return this.genResponse();
} else {
return this.res.redirect('/');
}
},
setAssets: function setAssets(params) {
params.jsBundle = this.manifest.javascript ? this.manifest.javascript[params.initScript] : this.manifest[params.initScript + '.js'];
params.cssBundle = this.manifest.styles ? this.manifest.styles[params.initScript] : this.manifest[params.initScript + '.css'];
return params;
},
getSessionID: function getSessionID() {
return this.req ? this.req.sessionID : null;
},
getViewerContext: function getViewerContext() {
return this.viewerContext;
},
getBootloaderParams: function getBootloaderParams(filename) {
var initScript = '';
initScript += filename;
var params = {
initScript: initScript
};
if (this.manifest) {
return this.setAssets(params);
} else {
return params;
}
},
genResponse: function genResponse() {
console.error('Implement me!');
}
});
module.exports = BaseController;
})();