Fixes Dropbox authentication (now calling reset() when needed, and properly done asynchronously)

This commit is contained in:
Thomas Tempelmann
2015-06-16 11:42:36 +02:00
parent 580311c8b4
commit 114f122186
2 changed files with 59 additions and 54 deletions
+6 -6
View File
@@ -18,13 +18,13 @@ var CONFIG = {
/* /*
* The key below needs to be set individually by you if you want to use the Dropbox load/save feature. * The key below needs to be set individually by you if you want to use the Dropbox load/save feature.
* To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to * To do that, first sign up with Dropbox (may require a specific developer / SDK sign-up), go to
* https://www.dropbox.com/developers/apps and use "Create app" to add a new app. Call it, for instance, * https://www.dropbox.com/developers/apps and use "Create app" to add a new "Dropbox API app".
* "wwwsqldesigner", and give it the "App Folder" permission. Unter "OAuth 2", "Redirect URIs", add * Limit the app to its own folder. Call it, for instance, "wwwsqldesigner".
* the URL to the "dropbox-oauth-receiver.html" file on your server. E.g, if you install wwwsqldesigner * Under "OAuth 2", "Redirect URIs", add the URL to the "dropbox-oauth-receiver.html" file on your server.
* on your local web server under "http://localhost/sqldesigner/", then add * E.g, if you install wwwsqldesigner on your local web server under "http://localhost/sqldesigner/", then add
* http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI. * http://localhost/sqldesigner/dropbox-oauth-receiver.html as a Redirection URI.
* Copy the shown "App key" and paste it here below: * Copy the shown "App key" and paste it here below instead of the null value:
*/ */
DROPBOX_KEY: null // "your app key" DROPBOX_KEY: null // such as: "d6stdscwewhl6sa"
} }
+53 -48
View File
@@ -250,7 +250,8 @@ SQL.IO.prototype.showDropboxError = function(error) {
// If you're using dropbox.js, the only cause behind this error is that // If you're using dropbox.js, the only cause behind this error is that
// the user token expired. // the user token expired.
// Get the user through the authentication flow again. // Get the user through the authentication flow again.
msg = _("Invalid Token (expired)"); msg = _("Token expired - retry the operation, authenticating again with Dropbox");
this.dropboxClient.reset();
break; break;
case Dropbox.ApiError.NOT_FOUND: case Dropbox.ApiError.NOT_FOUND:
@@ -288,76 +289,80 @@ SQL.IO.prototype.showDropboxError = function(error) {
alert (prefix+msg); alert (prefix+msg);
}; };
SQL.IO.prototype.showDropboxAuthenticate = function() { SQL.IO.prototype.showDropboxAuthenticate = function(connectedCallBack) {
if (!this.dropboxClient) return false; if (!this.dropboxClient) return false;
// We want to use a popup window for authentication as the default redirection won't work for us as it'll make us lose our schema data // We want to use a popup window for authentication as the default redirection won't work for us as it'll make us lose our schema data
this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: "dropbox-oauth-receiver.html" })); var href = window.location.href;
var prefix = href.substring(0, href.lastIndexOf('/')) + "/";
this.dropboxClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl: prefix+"dropbox-oauth-receiver.html" }));
// Now let's authenticate us // Now let's authenticate us
var success = false; var sql_io = this;
this.dropboxClient.authenticate( function(error, client) { sql_io.dropboxClient.authenticate( function(error, client) {
if (error) { if (error) {
this.showDropboxError(error); sql_io.showDropboxError(error);
return; } else {
// We're authenticated
connectedCallBack();
} }
success = true;
return; return;
}); });
return success;
return true;
} }
SQL.IO.prototype.dropboxsave = function() { SQL.IO.prototype.dropboxsave = function() {
if (!this.showDropboxAuthenticate()) return;
var key = this.promptName("serversaveprompt", ".xml");
if (!key) { return; }
var filename = (key || "default") + ".xml";
var sql_io = this; var sql_io = this;
sql_io.listresponse("Saving...", 200); sql_io.showDropboxAuthenticate( function() {
var xml = this.owner.toXML(); var key = sql_io.promptName("serversaveprompt", ".xml");
this.dropboxClient.writeFile(filename, xml, function(error, stat) { if (!key) { return; }
if (error) {
sql_io.listresponse("", 200); var filename = (key || "default") + ".xml";
return this.showDropboxError(error);
} sql_io.listresponse("Saving...", 200);
sql_io.listresponse(filename+" "+_("was saved to Dropbox"), 200); var xml = sql_io.owner.toXML();
sql_io.dropboxClient.writeFile(filename, xml, function(error, stat) {
if (error) {
sql_io.listresponse("", 200);
return sql_io.showDropboxError(error);
}
sql_io.listresponse(filename+" "+_("was saved to Dropbox"), 200);
});
}); });
} }
SQL.IO.prototype.dropboxload = function() { SQL.IO.prototype.dropboxload = function() {
if (!this.showDropboxAuthenticate()) return;
var key = this.promptName("serverloadprompt", ".xml");
if (!key) { return; }
var filename = (key || "default") + ".xml";
var sql_io = this; var sql_io = this;
sql_io.listresponse("Loading...", 200); sql_io.showDropboxAuthenticate( function() {
this.dropboxClient.readFile(filename, function(error, data) { var key = sql_io.promptName("serverloadprompt", ".xml");
sql_io.listresponse("", 200); if (!key) { return; }
if (error) {
return this.showDropboxError(error); var filename = (key || "default") + ".xml";
}
sql_io.fromXMLText(data); sql_io.listresponse("Loading...", 200);
sql_io.dropboxClient.readFile(filename, function(error, data) {
sql_io.listresponse("", 200);
if (error) {
return sql_io.showDropboxError(error);
}
sql_io.fromXMLText(data);
});
}); });
} }
SQL.IO.prototype.dropboxlist = function() { SQL.IO.prototype.dropboxlist = function() {
if (!this.showDropboxAuthenticate()) return;
var sql_io = this; var sql_io = this;
sql_io.listresponse("Loading...", 200); sql_io.showDropboxAuthenticate( function() {
this.dropboxClient.readdir("/", function(error, entries) { sql_io.listresponse("Loading...", 200);
if (error) { sql_io.dropboxClient.readdir("/", function(error, entries) {
sql_io.listresponse("", 200); if (error) {
return this.showDropboxError(error); sql_io.listresponse("", 200);
} return sql_io.showDropboxError(error);
var data = entries.join("\n")+"\n"; }
sql_io.listresponse(data, 200); var data = entries.join("\n")+"\n";
sql_io.listresponse(data, 200);
});
}); });
} }