Refactor login to remove duplicate code
parent
c2e9c60756
commit
5bba9b6c39
|
@ -6,10 +6,17 @@ define([
|
|||
'/common/outer/network-config.js',
|
||||
'/customize/credential.js',
|
||||
'/bower_components/chainpad/chainpad.dist.js',
|
||||
'/common/common-realtime.js',
|
||||
'/common/common-constants.js',
|
||||
'/common/common-interface.js',
|
||||
'/common/common-feedback.js',
|
||||
'/common/outer/local-store.js',
|
||||
'/customize/messages.js',
|
||||
|
||||
'/bower_components/tweetnacl/nacl-fast.min.js',
|
||||
'/bower_components/scrypt-async/scrypt-async.min.js', // better load speed
|
||||
], function ($, Listmap, Crypto, Util, NetConfig, Cred, ChainPad) {
|
||||
], function ($, Listmap, Crypto, Util, NetConfig, Cred, ChainPad, Realtime, Constants, UI,
|
||||
Feedback, LocalStore, Messages) {
|
||||
var Exports = {
|
||||
Cred: Cred,
|
||||
};
|
||||
|
@ -142,5 +149,127 @@ define([
|
|||
});
|
||||
};
|
||||
|
||||
Exports.loginOrRegisterUI = function (uname, passwd, isRegister, shouldImport, testing, test) {
|
||||
var hashing = true;
|
||||
|
||||
var proceed = function (result) {
|
||||
var proxy = result.proxy;
|
||||
proxy.edPublic = result.edPublic;
|
||||
proxy.edPrivate = result.edPrivate;
|
||||
proxy.curvePublic = result.curvePublic;
|
||||
proxy.curvePrivate = result.curvePrivate;
|
||||
|
||||
if (isRegister) {
|
||||
Feedback.send('REGISTRATION', true);
|
||||
} else {
|
||||
Feedback.send('LOGIN', true);
|
||||
}
|
||||
|
||||
Realtime.whenRealtimeSyncs(result.realtime, function () {
|
||||
try {
|
||||
LocalStore.login(result.userHash, result.userName, function () {
|
||||
hashing = false;
|
||||
if (test && typeof test === "function" && test()) { console.log('testing');
|
||||
return; }
|
||||
if (shouldImport) {
|
||||
sessionStorage.migrateAnonDrive = 1;
|
||||
}
|
||||
if (sessionStorage.redirectTo) {
|
||||
var h = sessionStorage.redirectTo;
|
||||
var parser = document.createElement('a');
|
||||
parser.href = h;
|
||||
if (parser.origin === window.location.origin) {
|
||||
delete sessionStorage.redirectTo;
|
||||
window.location.href = h;
|
||||
return;
|
||||
}
|
||||
}
|
||||
window.location.href = '/drive/';
|
||||
});
|
||||
} catch (e) { console.error(e); }
|
||||
});
|
||||
};
|
||||
|
||||
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen
|
||||
// pops up
|
||||
window.setTimeout(function () {
|
||||
UI.addLoadingScreen({
|
||||
loadingText: Messages.login_hashing,
|
||||
hideTips: true,
|
||||
});
|
||||
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed
|
||||
// after hashing the password
|
||||
window.setTimeout(function () {
|
||||
Exports.loginOrRegister(uname, passwd, isRegister, function (err, result) {
|
||||
var proxy;
|
||||
if (result) { proxy = result.proxy; }
|
||||
|
||||
if (err) {
|
||||
switch (err) {
|
||||
case 'NO_SUCH_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_noSuchUser, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalUser, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_PASS':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalPass, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'PASS_TOO_SHORT':
|
||||
UI.removeLoadingScreen(function () {
|
||||
var warning = Messages._getKey('register_passwordTooShort', [
|
||||
Cred.MINIMUM_PASSWORD_LENGTH
|
||||
]);
|
||||
UI.alert(warning, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'ALREADY_REGISTERED':
|
||||
// logMeIn should reset registering = false
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.confirm(Messages.register_alreadyRegistered, function (yes) {
|
||||
if (!yes) { return; }
|
||||
proxy.login_name = uname;
|
||||
|
||||
if (!proxy[Constants.displayNameKey]) {
|
||||
proxy[Constants.displayNameKey] = uname;
|
||||
}
|
||||
LocalStore.eraseTempSessionValues();
|
||||
proceed(result);
|
||||
});
|
||||
});
|
||||
break;
|
||||
default: // UNHANDLED ERROR
|
||||
hashing = false;
|
||||
UI.errorLoadingScreen(Messages.login_unhandledError);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (testing) { return void proceed(result); }
|
||||
|
||||
proxy.login_name = uname;
|
||||
proxy[Constants.displayNameKey] = uname;
|
||||
sessionStorage.createReadme = 1;
|
||||
|
||||
proceed(result);
|
||||
});
|
||||
}, 0);
|
||||
}, 200);
|
||||
};
|
||||
|
||||
return Exports;
|
||||
});
|
||||
|
|
|
@ -553,6 +553,7 @@ define([
|
|||
var $loading, $container;
|
||||
if ($('#' + LOADING).length) {
|
||||
$loading = $('#' + LOADING); //.show();
|
||||
$loading.css('display', '');
|
||||
$loading.removeClass('cp-loading-hidden');
|
||||
if (loadingText) {
|
||||
$('#' + LOADING).find('p').text(loadingText);
|
||||
|
|
|
@ -13,7 +13,6 @@ define([
|
|||
$(function () {
|
||||
var $main = $('#mainBlock');
|
||||
var $checkImport = $('#import-recent');
|
||||
var Messages = Cryptpad.Messages;
|
||||
|
||||
// main block is hidden in case javascript is disabled
|
||||
$main.removeClass('hidden');
|
||||
|
@ -61,90 +60,15 @@ define([
|
|||
|
||||
hashing = true;
|
||||
var shouldImport = $checkImport[0].checked;
|
||||
|
||||
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen pops up
|
||||
window.setTimeout(function () {
|
||||
UI.addLoadingScreen({
|
||||
loadingText: Messages.login_hashing,
|
||||
hideTips: true,
|
||||
});
|
||||
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed after hashing the password
|
||||
window.setTimeout(function () {
|
||||
loginReady(function () {
|
||||
var uname = $uname.val();
|
||||
var passwd = $passwd.val();
|
||||
Login.loginOrRegister(uname, passwd, false, function (err, result) {
|
||||
if (!err) {
|
||||
var proxy = result.proxy;
|
||||
|
||||
// successful validation and user already exists
|
||||
// set user hash in localStorage and redirect to drive
|
||||
if (!proxy.login_name) {
|
||||
result.proxy.login_name = result.userName;
|
||||
}
|
||||
|
||||
proxy.edPrivate = result.edPrivate;
|
||||
proxy.edPublic = result.edPublic;
|
||||
|
||||
proxy.curvePrivate = result.curvePrivate;
|
||||
proxy.curvePublic = result.curvePublic;
|
||||
|
||||
Feedback.send('LOGIN', true);
|
||||
Realtime.whenRealtimeSyncs(result.realtime, function() {
|
||||
LocalStore.login(result.userHash, result.userName, function () {
|
||||
hashing = false;
|
||||
if (test) {
|
||||
localStorage.clear();
|
||||
test.pass();
|
||||
return;
|
||||
}
|
||||
if (shouldImport) {
|
||||
sessionStorage.migrateAnonDrive = 1;
|
||||
}
|
||||
if (sessionStorage.redirectTo) {
|
||||
var h = sessionStorage.redirectTo;
|
||||
var parser = document.createElement('a');
|
||||
parser.href = h;
|
||||
if (parser.origin === window.location.origin) {
|
||||
delete sessionStorage.redirectTo;
|
||||
window.location.href = h;
|
||||
return;
|
||||
}
|
||||
}
|
||||
window.location.href = '/drive/';
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
switch (err) {
|
||||
case 'NO_SUCH_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_noSuchUser, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalUser, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_PASS':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalPass, function () {
|
||||
hashing = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
default: // UNHANDLED ERROR
|
||||
UI.errorLoadingScreen(Messages.login_unhandledError);
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 0);
|
||||
}, 100);
|
||||
var uname = $uname.val();
|
||||
var passwd = $passwd.val();
|
||||
Login.loginOrRegisterUI(uname, passwd, false, shouldImport, Test.testing, function () {
|
||||
if (test) {
|
||||
localStorage.clear();
|
||||
test.pass();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
$('#register').on('click', function () {
|
||||
if (sessionStorage) {
|
||||
|
|
|
@ -55,39 +55,6 @@ define([
|
|||
|
||||
var registering = false;
|
||||
var test;
|
||||
var logMeIn = function (result) {
|
||||
LocalStore.setUserHash(result.userHash);
|
||||
|
||||
var proxy = result.proxy;
|
||||
proxy.edPublic = result.edPublic;
|
||||
proxy.edPrivate = result.edPrivate;
|
||||
proxy.curvePublic = result.curvePublic;
|
||||
proxy.curvePrivate = result.curvePrivate;
|
||||
|
||||
Feedback.send('REGISTRATION', true);
|
||||
|
||||
Realtime.whenRealtimeSyncs(result.realtime, function () {
|
||||
LocalStore.login(result.userHash, result.userName, function () {
|
||||
registering = false;
|
||||
if (test) {
|
||||
localStorage.clear();
|
||||
test.pass();
|
||||
return;
|
||||
}
|
||||
if (sessionStorage.redirectTo) {
|
||||
var h = sessionStorage.redirectTo;
|
||||
var parser = document.createElement('a');
|
||||
parser.href = h;
|
||||
if (parser.origin === window.location.origin) {
|
||||
delete sessionStorage.redirectTo;
|
||||
window.location.href = h;
|
||||
return;
|
||||
}
|
||||
}
|
||||
window.location.href = '/drive/';
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$register.click(function () {
|
||||
if (registering) {
|
||||
|
@ -125,89 +92,14 @@ define([
|
|||
function (yes) {
|
||||
if (!yes) { return; }
|
||||
|
||||
Login.loginOrRegisterUI(uname, passwd, true, shouldImport, Test.testing, function () {
|
||||
if (test) {
|
||||
localStorage.clear();
|
||||
test.pass();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
registering = true;
|
||||
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen pops up
|
||||
window.setTimeout(function () {
|
||||
UI.addLoadingScreen({
|
||||
loadingText: Messages.login_hashing,
|
||||
hideTips: true,
|
||||
});
|
||||
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed after hashing the password
|
||||
window.setTimeout(function () {
|
||||
Login.loginOrRegister(uname, passwd, true, function (err, result) {
|
||||
var proxy;
|
||||
if (result) { proxy = result.proxy; }
|
||||
|
||||
if (err) {
|
||||
switch (err) {
|
||||
case 'NO_SUCH_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_noSuchUser, function () {
|
||||
registering = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_USER':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalUser, function () {
|
||||
registering = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'INVAL_PASS':
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.alert(Messages.login_invalPass, function () {
|
||||
registering = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'PASS_TOO_SHORT':
|
||||
UI.removeLoadingScreen(function () {
|
||||
var warning = Messages._getKey('register_passwordTooShort', [
|
||||
Cred.MINIMUM_PASSWORD_LENGTH
|
||||
]);
|
||||
UI.alert(warning, function () {
|
||||
registering = false;
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'ALREADY_REGISTERED':
|
||||
// logMeIn should reset registering = false
|
||||
UI.removeLoadingScreen(function () {
|
||||
UI.confirm(Messages.register_alreadyRegistered, function (yes) {
|
||||
if (!yes) { return; }
|
||||
proxy.login_name = uname;
|
||||
|
||||
if (!proxy[Constants.displayNameKey]) {
|
||||
proxy[Constants.displayNameKey] = uname;
|
||||
}
|
||||
LocalStore.eraseTempSessionValues();
|
||||
logMeIn(result);
|
||||
});
|
||||
});
|
||||
break;
|
||||
default: // UNHANDLED ERROR
|
||||
registering = false;
|
||||
UI.errorLoadingScreen(Messages.login_unhandledError);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (Test.testing) { return void logMeIn(result); }
|
||||
|
||||
LocalStore.eraseTempSessionValues();
|
||||
if (shouldImport) {
|
||||
sessionStorage.migrateAnonDrive = 1;
|
||||
}
|
||||
|
||||
proxy.login_name = uname;
|
||||
proxy[Constants.displayNameKey] = uname;
|
||||
sessionStorage.createReadme = 1;
|
||||
|
||||
logMeIn(result);
|
||||
});
|
||||
}, 0);
|
||||
}, 200);
|
||||
}, {
|
||||
ok: Messages.register_writtenPassword,
|
||||
cancel: Messages.register_cancel,
|
||||
|
|
Loading…
Reference in New Issue