From dceb296f559d31d0350a105f77e693f791bce3a2 Mon Sep 17 00:00:00 2001 From: Bernat Romagosa Date: Wed, 4 Oct 2017 19:53:25 +0200 Subject: [PATCH] persistent login --- cloud.js | 27 ++++++++++++++++++++------- gui.js | 40 +++++++++------------------------------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/cloud.js b/cloud.js index 70152a88..e3e49c75 100644 --- a/cloud.js +++ b/cloud.js @@ -46,12 +46,8 @@ function Cloud(url) { Cloud.prototype.init = function (url) { this.url = url; this.username = null; - this.checkCredentials(); }; -Cloud.prototype.clear = function () { - this.username = null; -}; // Dictionary handling @@ -193,6 +189,18 @@ Cloud.prototype.withCredentialsRequest = function ( // Credentials management +Cloud.prototype.initSession = function (onSuccess) { + var myself = this; + this.request( + 'POST', + '/init', + function () { myself.checkCredentials(onSuccess); }, + nop, + null, + true + ); +}; + Cloud.prototype.checkCredentials = function (onSuccess, onError) { var myself = this; this.getCurrentUser( @@ -211,20 +219,25 @@ Cloud.prototype.getCurrentUser = function (onSuccess, onError) { }; Cloud.prototype.logout = function (onSuccess, onError) { + this.username = null; this.request( 'POST', - '/users/' + this.username + '/logout', + '/logout', onSuccess, onError, 'logout failed' ); }; -Cloud.prototype.login = function (username, password, onSuccess, onError) { +Cloud.prototype.login = function (username, password, persist, onSuccess, onError) { var myself = this; this.request( 'POST', - '/users/' + username + '/login?' + this.encodeDict({ password: password }), + '/users/' + username + '/login?' + + this.encodeDict({ + password: password, + persist: persist + }), function () { myself.checkCredentials(onSuccess, onError); }, diff --git a/gui.js b/gui.js index a404a2b8..42f4c5d5 100644 --- a/gui.js +++ b/gui.js @@ -268,22 +268,13 @@ IDE_Morph.prototype.init = function (isAutoFill) { IDE_Morph.prototype.openIn = function (world) { var hash, usr, myself = this, urlLanguage = null; - // get persistent user data, if any - if (this.hasLocalStorage()) { - usr = localStorage['-snap-user']; - if (usr) { - SnapCloud.checkCredentials( - function (username) { - if (username) { - this.source = 'cloud'; - } - }, - function () { - delete localStorage['-snap-user']; - } - ); + SnapCloud.initSession( + function (username) { + if (username) { + myself.source = 'cloud'; + } } - } + ); this.buildPanes(); world.add(this); @@ -5055,10 +5046,8 @@ IDE_Morph.prototype.initializeCloud = function () { SnapCloud.login( user.username, user.password, + user.choice, function () { - if (user.choice) { - localStorage['-snap-user'] = user.username; - } myself.source = 'cloud'; myself.showMessage('now connected.', 2); }, @@ -5082,11 +5071,7 @@ IDE_Morph.prototype.initializeCloud = function () { IDE_Morph.prototype.createCloudAccount = function () { var myself = this, world = this.world(); -/* - // force-logout, commented out for now: - delete localStorage['-snap-user']; - SnapCloud.clear(); -*/ + new DialogBoxMorph( null, function (user) { @@ -5124,11 +5109,7 @@ IDE_Morph.prototype.createCloudAccount = function () { IDE_Morph.prototype.resetCloudPassword = function () { var myself = this, world = this.world(); -/* - // force-logout, commented out for now: - delete localStorage['-snap-user']; - SnapCloud.clear(); -*/ + new DialogBoxMorph( null, function (user) { @@ -5194,14 +5175,11 @@ IDE_Morph.prototype.changeCloudPassword = function () { IDE_Morph.prototype.logout = function () { var myself = this; - delete localStorage['-snap-user']; SnapCloud.logout( function () { - SnapCloud.clear(); myself.showMessage('disconnected.', 2); }, function () { - SnapCloud.clear(); myself.showMessage('disconnected.', 2); } );