Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
server.ext('onPreResponse', (request, reply) => {
// Only interested in errors
if (!(request.response instanceof Error)) return reply.continue();
const error = Boom.wrap(request.response);
const accept = request.headers.accept || '';
if (error.output.statusCode >= 500) {
console.error(error.stack);
}
if (accept.indexOf('text/html') === 0) {
// Respond with an error template
if (error.output.statusCode === 401) {
return reply.redirect('/login');
}
if (error.output.statusCode === 404) {
var data = {};
data.footer = require('../../fixtures/footer');
data.museums = require('../../fixtures/museums');
// Transform the original options using options hook
var options = GeneralUtil.applyOptionsHook(request, origOptions);
var RequestState = request.plugins.bedwetter = {
action: 'remove',
options: options
};
var actionUtil = require('../actionUtil')(request, options);
// Ensure a model and alias can be deduced from the request.
var Model = actionUtil.parseModel();
var relation = options.associationAttr;
if (!relation) {
return reply(Boom.wrap(new Error('Missing required route option, `options.associationAttr`.')));
}
var associationAttr = _.findWhere(options.associations, { alias: relation });
Hoek.assert(_.isObject(associationAttr), 'Bad association.');
var ChildModel = request.model[associationAttr.collection];
var childPkAttr = ChildModel.primaryKey;
// The primary key of the parent record
var parentPk;
try {
parentPk = actionUtil.requirePk();
} catch(e) {
return reply(Boom.wrap(e));
}
.catch(reason => reply(Boom.wrap(reason)));
}
withManifestAndInfo(req, {peer: true}, (err, manifest, info) => {
if (err) return next(Boom.wrap(err, 500, 'Failed to get manifest and info'))
const payload = merge({manifest: minifyManifest(manifest), info}, projectData(req, 'peer'))
res.json(payload)
})
})
}, function (err, entities) {
if (err) {
return next(boom.wrap(err));
}
if (!entities) {
return next(boom.notFound());
}
req.data = entities;
done(null, req, res, next);
});
}
export async function createAndReplyWithSyncbackRequest(request, reply, syncRequestArgs = {}) {
try {
const account = request.auth.credentials
const {wakeSync = true} = syncRequestArgs
syncRequestArgs.accountId = account.id
const db = await request.getAccountDatabase()
const syncbackRequest = await db.SyncbackRequest.create(syncRequestArgs)
if (wakeSync) {
wakeSyncWorker(account.id, `Need to run task ${syncbackRequest.type}`)
}
reply(Serialization.jsonStringify(syncbackRequest))
return syncbackRequest
} catch (err) {
reply(Boom.wrap(err))
return null
}
}
req.session.set('session/access-token', data.access_token, (err) => {
if (err) return next(Boom.wrap(err, 500, 'Failed to store access token in session'))
req.session.set('session/user', data.user, (err) => {
if (err) return next(Boom.wrap(err, 500, 'Failed to store user in session'))
res.redirect('/?success')
})
})
})