From 281239dea6c0710f90c530959df609c2289f8898 Mon Sep 17 00:00:00 2001 From: "Jochen H. Schmidt" Date: Sun, 2 Sep 2018 23:01:24 +0200 Subject: [PATCH] Use referer for redirect If no options.redirect is given, try using the referer of the request for the redirect to the original page. Make referer redirect configurable --- controllers/process.js | 2 ++ lib/Staticman.js | 4 +++- siteConfig.js | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/controllers/process.js b/controllers/process.js index 62570762..c6b9c224 100644 --- a/controllers/process.js +++ b/controllers/process.js @@ -68,6 +68,8 @@ function process (staticman, req, res) { const fields = req.query.fields || req.body.fields const options = req.query.options || req.body.options || {} + options.referer = req.headers.referer + return staticman.processEntry(fields, options).then(data => { sendResponse(res, { redirect: data.redirect, diff --git a/lib/Staticman.js b/lib/Staticman.js index 5a56eb59..a60b1227 100644 --- a/lib/Staticman.js +++ b/lib/Staticman.js @@ -513,9 +513,11 @@ Staticman.prototype.processEntry = function (fields, options) { commitMessage ) }).then(result => { + let referer = this.siteConfig.get("referer") + let redirect = (referer.enabled && options.referer)? options.referer+referer.postfix : options.redirect || false return { fields: fields, - redirect: options.redirect ? options.redirect : false + redirect: redirect } }).catch(err => { return Promise.reject(errorHandler('ERROR_PROCESSING_ENTRY', { diff --git a/siteConfig.js b/siteConfig.js index 864cef50..aefe8a7a 100644 --- a/siteConfig.js +++ b/siteConfig.js @@ -156,6 +156,18 @@ const schema = { format: 'EncryptedString', default: '' } + }, + referer: { + enabled: { + doc: 'Set to `true` to enable redirect back to referer page', + format: Boolean, + default: false + }, + postfix: { + doc: 'Append this string to the referer to build the redirect adress', + format: String, + default: '' + } } }