From f22960ad5925300fedd14143fae5f4adbb72ceb3 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 19 Oct 2018 14:03:22 +0100 Subject: [PATCH] Avoid http redirects as Safari doesn't reuse Auth header Fixes #1903 --- .../editor-api/lib/editor/projects.js | 128 +++++++++--------- 1 file changed, 66 insertions(+), 62 deletions(-) diff --git a/packages/node_modules/@node-red/editor-api/lib/editor/projects.js b/packages/node_modules/@node-red/editor-api/lib/editor/projects.js index 468580459..ff7fc5e85 100644 --- a/packages/node_modules/@node-red/editor-api/lib/editor/projects.js +++ b/packages/node_modules/@node-red/editor-api/lib/editor/projects.js @@ -20,6 +20,58 @@ var apiUtils = require("../util"); var runtimeAPI; var needsPermission = require("../auth").needsPermission; +function listProjects(req,res) { + var opts = { + user: req.user + } + runtimeAPI.projects.listProjects(opts).then(function(result) { + res.json(result); + }).catch(function(err) { + apiUtils.rejectHandler(req,res,err); + }); +} +function getProject(req,res) { + var opts = { + user: req.user, + id: req.params.id + } + runtimeAPI.projects.getProject(opts).then(function(data) { + if (data) { + res.json(data); + } else { + res.status(404).end(); + } + }).catch(function(err) { + apiUtils.rejectHandler(req,res,err); + }) +} +function getProjectStatus(req,res) { + var opts = { + user: req.user, + id: req.params.id, + remote: req.query.remote + } + runtimeAPI.projects.getStatus(opts).then(function(data){ + if (data) { + res.json(data); + } else { + res.status(404).end(); + } + }).catch(function(err) { + apiUtils.rejectHandler(req,res,err); + }) +} +function getProjectRemotes(req,res) { + var opts = { + user: req.user, + id: req.params.id + } + runtimeAPI.projects.getRemotes(opts).then(function(data) { + res.json(data); + }).catch(function(err) { + apiUtils.rejectHandler(req,res,err); + }) +} module.exports = { init: function(_runtimeAPI) { runtimeAPI = _runtimeAPI; @@ -40,16 +92,7 @@ module.exports = { // Projects // List all projects - app.get("/", needsPermission("projects.read"), function(req,res) { - var opts = { - user: req.user - } - runtimeAPI.projects.listProjects(opts).then(function(result) { - res.json(result); - }).catch(function(err) { - apiUtils.rejectHandler(req,res,err); - }); - }); + app.get("/", needsPermission("projects.read"),listProjects); // Create project app.post("/", needsPermission("projects.write"), function(req,res) { @@ -74,13 +117,13 @@ module.exports = { if (req.body.active) { runtimeAPI.projects.setActiveProject(opts).then(function() { - res.redirect(303,req.baseUrl + '/'); + listProjects(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) } else if (req.body.initialise) { runtimeAPI.projects.initialiseProject(opts).then(function() { - res.redirect(303,req.baseUrl + '/'+ req.params.id); + getProject(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -91,7 +134,7 @@ module.exports = { req.body.hasOwnProperty('files') || req.body.hasOwnProperty('git')) { runtimeAPI.projects.updateProject(opts).then(function() { - res.redirect(303,req.baseUrl + '/'+ req.params.id); + getProject(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -101,21 +144,7 @@ module.exports = { }); // Get project metadata - app.get("/:id", needsPermission("projects.read"), function(req,res) { - var opts = { - user: req.user, - id: req.params.id - } - runtimeAPI.projects.getProject(opts).then(function(data) { - if (data) { - res.json(data); - } else { - res.status(404).end(); - } - }).catch(function(err) { - apiUtils.rejectHandler(req,res,err); - }) - }); + app.get("/:id", needsPermission("projects.read"), getProject); // Delete project app.delete("/:id", needsPermission("projects.write"), function(req,res) { @@ -132,22 +161,7 @@ module.exports = { // Get project status - files, commit counts, branch info - app.get("/:id/status", needsPermission("projects.read"), function(req,res) { - var opts = { - user: req.user, - id: req.params.id, - remote: req.query.remote - } - runtimeAPI.projects.getStatus(opts).then(function(data){ - if (data) { - res.json(data); - } else { - res.status(404).end(); - } - }).catch(function(err) { - apiUtils.rejectHandler(req,res,err); - }) - }); + app.get("/:id/status", needsPermission("projects.read"), getProjectStatus); // Project file listing @@ -203,7 +217,7 @@ module.exports = { path: req.params[0] } runtimeAPI.projects.stageFile(opts).then(function() { - res.redirect(303,req.baseUrl+"/"+opts.id+"/status"); + getProjectStatus(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -217,7 +231,7 @@ module.exports = { path: req.body.files } runtimeAPI.projects.stageFile(opts).then(function() { - res.redirect(303,req.baseUrl+"/"+opts.id+"/status"); + getProjectStatus(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -231,7 +245,7 @@ module.exports = { message: req.body.message } runtimeAPI.projects.commit(opts).then(function() { - res.redirect(303,req.baseUrl+"/"+opts.id+"/status"); + getProjectStatus(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -245,7 +259,7 @@ module.exports = { path: req.params[0] } runtimeAPI.projects.unstageFile(opts).then(function() { - res.redirect(303,req.baseUrl+"/"+opts.id+"/status"); + getProjectStatus(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -258,7 +272,7 @@ module.exports = { id: req.params.id } runtimeAPI.projects.unstageFile(opts).then(function() { - res.redirect(303,req.baseUrl+"/"+opts.id+"/status"); + getProjectStatus(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -442,17 +456,7 @@ module.exports = { }); // Get a list of remotes - app.get("/:id/remotes", needsPermission("projects.read"), function(req, res) { - var opts = { - user: req.user, - id: req.params.id - } - runtimeAPI.projects.getRemotes(opts).then(function(data) { - res.json(data); - }).catch(function(err) { - apiUtils.rejectHandler(req,res,err); - }) - }); + app.get("/:id/remotes", needsPermission("projects.read"), getProjectRemotes); // Add a remote app.post("/:id/remotes", needsPermission("projects.write"), function(req,res) { @@ -466,7 +470,7 @@ module.exports = { return; } runtimeAPI.projects.addRemote(opts).then(function(data) { - res.redirect(303,req.baseUrl+"/"+opts.id+"/remotes"); + getProjectRemotes(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }) @@ -480,7 +484,7 @@ module.exports = { remote: req.params.remoteName } runtimeAPI.projects.removeRemote(opts).then(function(data) { - res.redirect(303,req.baseUrl+"/"+opts.id+"/remotes"); + getProjectRemotes(req,res); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); })