Permalink
Browse files

Refactored middleware for Dropbox authentication

  • Loading branch information...
davidmerfield committed Nov 20, 2018
1 parent 0b5869b commit dc8d257ae97a8596434d45f5fd8bef05ee2ab1d5
@@ -44,9 +44,17 @@ dashboard
.get(require("./checkAppFolder"))
.get(require("./askToMigrateIfNeeded"))
.get(require("./createFolder"))
.get(require("./writeExistingContents"))
.get(require("./saveDropboxAccount"))
.get(function(req, res, next) {
res.message("/", "Set up Dropbox successfuly!");
require("./writeExistingContents")(req, res, function(err) {
// Headers have been sent at this point, so just log this error
if (err) console.log("Dropbox authentication error:", err);
console.log("Blog:");
});
})
// If we encounter some error during
// the authentication flow, send them
// back to the setup page where they started
@@ -70,9 +78,17 @@ dashboard
})
.post(require("./moveExistingFiles"))
.post(require("./createFolder"))
.post(require("./writeExistingContents"))
.post(require("./saveDropboxAccount"));
.post(require("./saveDropboxAccount"))
.post(function(req, res, next) {
res.message("/", "Set up Dropbox successfuly!");
require("./writeExistingContents")(req, res, function(err) {
// Headers have been sent at this point, so just log this error
if (err) console.log("Dropbox authentication error:", err);
console.log("Blog:");
});
});
// Will remove the Dropbox account from the client's database
// and revoke the token if needed.
dashboard
@@ -1,17 +1,4 @@
var database = require("../database");
module.exports = function saveDropboxAccount(req, res, next) {
console.log(
"Blog:",
req.blog.id,
"Saving new Dropbox account with full access:",
req.unsavedAccount.full_access
);
database.set(req.blog.id, req.unsavedAccount, function(err) {
if (err) return next(err);
res.message("/", "Set up Dropbox successfuly!");
});
database.set(req.blog.id, req.unsavedAccount, next);
};
@@ -37,6 +37,7 @@ module.exports = function main(blog, callback) {
// blog folder in the user's Dropbox folder.
delta(account.cursor, function handle(err, result) {
if (err) {
debug("Delta error", err);
return Database.set(
blog.id,
{ error_code: err.status || 400 },
@@ -46,6 +47,8 @@ module.exports = function main(blog, callback) {
);
}
debug("Retrieved", result.entries.length, "changes");
// Now we attempt to apply the changes which occured in the
// user's folder on Dropbox to the blog folder on Blot's server.
// This means making any new directories, downloading any new
@@ -102,7 +105,10 @@ module.exports = function main(blog, callback) {
// we get them before returning the callback.
// This is important because a rename could
// be split across two pages of file events.
if (result.has_more) return delta(result.cursor, handle);
if (result.has_more) {
debug("There are more change to fetch");
return delta(result.cursor, handle);
}
debug("Finished sync!");
done(null, callback);

0 comments on commit dc8d257

Please sign in to comment.