Fix race conditon

pull/1/head
yflory 8 years ago
parent 94417d9a8b
commit dd014f6ef2

@ -16,11 +16,12 @@ define([
var $ = window.jQuery; var $ = window.jQuery;
var saveAs = window.saveAs; var saveAs = window.saveAs;
var $iframe = $('#pad-iframe').contents();
var ifrw = $('#pad-iframe')[0].contentWindow;
// Use `$(function () {});` to make sure the html is loaded before doing anything else
$(function () {
var $iframe = $('#pad-iframe').contents();
var ifrw = $('#pad-iframe')[0].contentWindow;
Cryptpad.addLoadingScreen(); Cryptpad.addLoadingScreen();
var onConnectError = function (info) { var onConnectError = function (info) {
@ -2032,5 +2033,5 @@ define([
} }
}); });
});
}); });

@ -39,101 +39,101 @@ define([
} else { } else {
$main.find('#userForm').removeClass('hidden'); $main.find('#userForm').removeClass('hidden');
} }
});
// text and password input fields // text and password input fields
var $uname = $('#username'); var $uname = $('#username');
var $passwd = $('#password'); var $passwd = $('#password');
var $confirm = $('#password-confirm'); var $confirm = $('#password-confirm');
if (sessionStorage.login_user) { if (sessionStorage.login_user) {
$uname.val(sessionStorage.login_user); $uname.val(sessionStorage.login_user);
} }
if (sessionStorage.login_pass) { if (sessionStorage.login_pass) {
$passwd.val(sessionStorage.login_pass); $passwd.val(sessionStorage.login_pass);
} }
[ $uname, $passwd, $confirm] [ $uname, $passwd, $confirm]
.some(function ($el) { if (!$el.val()) { $el.focus(); return true; } }); .some(function ($el) { if (!$el.val()) { $el.focus(); return true; } });
// checkboxes // checkboxes
var $checkImport = $('#import-recent'); var $checkImport = $('#import-recent');
var $checkAcceptTerms = $('#accept-terms'); var $checkAcceptTerms = $('#accept-terms');
var $checkPromise = $('#promise'); var $checkPromise = $('#promise');
var $register = $('button#register'); var $register = $('button#register');
$register.click(function () { $register.click(function () {
var uname = $uname.val(); var uname = $uname.val();
var passwd = $passwd.val(); var passwd = $passwd.val();
var confirmPassword = $confirm.val(); var confirmPassword = $confirm.val();
var shouldImport = $checkImport[0].checked; var shouldImport = $checkImport[0].checked;
var doesAccept = $checkAcceptTerms[0].checked; var doesAccept = $checkAcceptTerms[0].checked;
var doesPromise = $checkPromise[0].checked; var doesPromise = $checkPromise[0].checked;
/* basic validation */ /* basic validation */
if (passwd !== confirmPassword) { // do their passwords match? if (passwd !== confirmPassword) { // do their passwords match?
return void Cryptpad.alert(Messages.register_passwordsDontMatch); return void Cryptpad.alert(Messages.register_passwordsDontMatch);
} }
if (!doesAccept) { // do they accept the terms of service? if (!doesAccept) { // do they accept the terms of service?
return void Cryptpad.alert(Messages.register_mustAcceptTerms); return void Cryptpad.alert(Messages.register_mustAcceptTerms);
} }
if (!doesPromise) { // do they promise to remember their password? if (!doesPromise) { // do they promise to remember their password?
return void Cryptpad.alert(Messages.register_mustRememberPass); return void Cryptpad.alert(Messages.register_mustRememberPass);
} }
Cryptpad.addLoadingScreen(Messages.login_hashing); Cryptpad.addLoadingScreen(Messages.login_hashing);
Login.loginOrRegister(uname, passwd, true, function (err, result) { Login.loginOrRegister(uname, passwd, true, function (err, result) {
if (err) { if (err) {
switch (err) { switch (err) {
case 'NO_SUCH_USER': case 'NO_SUCH_USER':
Cryptpad.removeLoadingScreen(function () { Cryptpad.removeLoadingScreen(function () {
Cryptpad.alert(Messages.login_noSuchUser); Cryptpad.alert(Messages.login_noSuchUser);
}); });
break; break;
case 'INVAL_USER': case 'INVAL_USER':
Cryptpad.removeLoadingScreen(function () { Cryptpad.removeLoadingScreen(function () {
Cryptpad.alert(Messages.login_invalUser); Cryptpad.alert(Messages.login_invalUser);
}); });
break; break;
case 'INVAL_PASS': case 'INVAL_PASS':
Cryptpad.removeLoadingScreen(function () { Cryptpad.removeLoadingScreen(function () {
Cryptpad.alert(Messages.login_invalPass); Cryptpad.alert(Messages.login_invalPass);
}); });
break; break;
default: // UNHANDLED ERROR default: // UNHANDLED ERROR
Cryptpad.errorLoadingScreen(Messages.login_unhandledError); Cryptpad.errorLoadingScreen(Messages.login_unhandledError);
}
} }
} var proxy = result.proxy;
var proxy = result.proxy;
localStorage.User_hash = result.userHash; localStorage.User_hash = result.userHash;
Cryptpad.eraseTempSessionValues(); Cryptpad.eraseTempSessionValues();
if (shouldImport) { if (shouldImport) {
sessionStorage.migrateAnonDrive = 1; sessionStorage.migrateAnonDrive = 1;
} }
proxy.login_name = uname; proxy.login_name = uname;
proxy[Cryptpad.displayNameKey] = uname; proxy[Cryptpad.displayNameKey] = uname;
proxy.initializing = true; proxy.initializing = true;
Cryptpad.whenRealtimeSyncs(result.realtime, function () { Cryptpad.whenRealtimeSyncs(result.realtime, function () {
Cryptpad.login(result.userHash, result.userName, function () { Cryptpad.login(result.userHash, result.userName, function () {
if (sessionStorage.redirectTo) { if (sessionStorage.redirectTo) {
var h = sessionStorage.redirectTo; var h = sessionStorage.redirectTo;
var parser = document.createElement('a'); var parser = document.createElement('a');
parser.href = h; parser.href = h;
if (parser.origin === window.location.origin) { if (parser.origin === window.location.origin) {
delete sessionStorage.redirectTo; delete sessionStorage.redirectTo;
window.location.href = h; window.location.href = h;
return; return;
}
} }
} window.location.href = '/drive/';
window.location.href = '/drive/'; });
}); });
}); });
}); });

Loading…
Cancel
Save