cryptpad/www/register/main.js

266 lines
11 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'/common/login.js',
'/common/cryptpad-common.js',
2017-06-01 08:23:50 +00:00
'/common/test.js',
'/common/credential.js', // preloaded for login.js
'less!/bower_components/components-font-awesome/css/font-awesome.min.css',
], function ($, Login, Cryptpad, Test, Cred) {
var Messages = Cryptpad.Messages;
$(function () {
var $main = $('#mainBlock');
// Language selector
var $sel = $('#language-selector');
Cryptpad.createLanguageSelector(undefined, $sel);
$sel.find('button').addClass('btn').addClass('btn-secondary');
$sel.show();
// User admin menu
var $userMenu = $('#user-menu');
var userMenuCfg = {
$initBlock: $userMenu
};
var $userAdmin = Cryptpad.createUserAdminMenu(userMenuCfg);
$userAdmin.find('button').addClass('btn').addClass('btn-secondary');
$(window).click(function () {
2017-09-04 13:09:54 +00:00
$('.cp-dropdown-content').hide();
});
// main block is hidden in case javascript is disabled
$main.removeClass('hidden');
// Make sure we don't display non-translated content (empty button)
$main.find('#data').removeClass('hidden');
if (Cryptpad.isLoggedIn()) {
// already logged in, redirect to drive
document.location.href = '/drive/';
return;
} else {
$main.find('#userForm').removeClass('hidden');
}
2017-02-17 14:16:03 +00:00
// text and password input fields
var $uname = $('#username');
var $passwd = $('#password');
var $confirm = $('#password-confirm');
2017-02-17 14:16:03 +00:00
if (sessionStorage.login_user) {
$uname.val(sessionStorage.login_user);
}
if (sessionStorage.login_pass) {
$passwd.val(sessionStorage.login_pass);
}
2017-02-17 14:16:03 +00:00
[ $uname, $passwd, $confirm]
.some(function ($el) { if (!$el.val()) { $el.focus(); return true; } });
2017-02-17 14:16:03 +00:00
// checkboxes
var $checkImport = $('#import-recent');
var $checkAcceptTerms = $('#accept-terms');
2017-02-17 14:16:03 +00:00
var $register = $('button#register');
2017-06-19 12:18:10 +00:00
var registering = false;
var logMeIn = function (result) {
2017-06-01 08:23:50 +00:00
if (Test.testing) {
Test.passed();
window.alert("Test passed!");
return;
}
localStorage.User_hash = result.userHash;
var proxy = result.proxy;
proxy.edPublic = result.edPublic;
proxy.edPrivate = result.edPrivate;
proxy.curvePublic = result.curvePublic;
proxy.curvePrivate = result.curvePrivate;
Cryptpad.feedback('REGISTRATION', true);
2017-04-14 16:21:11 +00:00
Cryptpad.whenRealtimeSyncs(result.realtime, function () {
Cryptpad.login(result.userHash, result.userName, function () {
2017-06-19 12:18:10 +00:00
registering = false;
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/';
});
});
};
2017-02-17 14:16:03 +00:00
$register.click(function () {
2017-06-19 12:18:10 +00:00
if (registering) {
console.log("registration is already in progress");
return;
}
2017-02-17 14:16:03 +00:00
var uname = $uname.val();
var passwd = $passwd.val();
var confirmPassword = $confirm.val();
2017-02-17 14:16:03 +00:00
var shouldImport = $checkImport[0].checked;
var doesAccept = $checkAcceptTerms[0].checked;
2017-02-17 14:16:03 +00:00
/* basic validation */
if (!Cred.isLongEnoughPassword(passwd)) {
var warning = Messages._getKey('register_passwordTooShort', [
Cred.MINIMUM_PASSWORD_LENGTH
]);
return void Cryptpad.alert(warning, function () {
registering = false;
});
}
2017-02-17 14:16:03 +00:00
if (passwd !== confirmPassword) { // do their passwords match?
return void Cryptpad.alert(Messages.register_passwordsDontMatch);
}
2017-02-17 14:16:03 +00:00
if (!doesAccept) { // do they accept the terms of service?
return void Cryptpad.alert(Messages.register_mustAcceptTerms);
}
2017-07-04 12:56:03 +00:00
setTimeout(function () {
2017-08-04 13:45:30 +00:00
Cryptpad.confirm("<h2 class='bright msg'>" + Messages.register_warning + "</h2>",
2017-03-14 11:00:23 +00:00
function (yes) {
if (!yes) { return; }
2017-06-19 12:18:10 +00:00
registering = true;
2017-05-10 13:54:09 +00:00
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen pops up
window.setTimeout(function () {
Cryptpad.addLoadingScreen({
loadingText: Messages.login_hashing,
hideTips: true,
});
2017-05-10 13:54:09 +00:00
// 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; }
2017-05-10 13:54:09 +00:00
if (err) {
switch (err) {
case 'NO_SUCH_USER':
Cryptpad.removeLoadingScreen(function () {
2017-06-19 12:18:10 +00:00
Cryptpad.alert(Messages.login_noSuchUser, function () {
registering = false;
});
2017-05-10 13:54:09 +00:00
});
break;
case 'INVAL_USER':
Cryptpad.removeLoadingScreen(function () {
2017-06-19 12:18:10 +00:00
Cryptpad.alert(Messages.login_invalUser, function () {
registering = false;
});
2017-05-10 13:54:09 +00:00
});
break;
case 'INVAL_PASS':
Cryptpad.removeLoadingScreen(function () {
2017-06-19 12:18:10 +00:00
Cryptpad.alert(Messages.login_invalPass, function () {
registering = false;
});
2017-05-10 13:54:09 +00:00
});
break;
case 'PASS_TOO_SHORT':
Cryptpad.removeLoadingScreen(function () {
var warning = Messages._getKey('register_passwordTooShort', [
Cred.MINIMUM_PASSWORD_LENGTH
]);
Cryptpad.alert(warning, function () {
registering = false;
});
});
break;
2017-05-10 13:54:09 +00:00
case 'ALREADY_REGISTERED':
2017-06-19 12:18:10 +00:00
// logMeIn should reset registering = false
2017-05-10 13:54:09 +00:00
Cryptpad.removeLoadingScreen(function () {
Cryptpad.confirm(Messages.register_alreadyRegistered, function (yes) {
if (!yes) { return; }
2017-05-10 13:54:09 +00:00
proxy.login_name = uname;
if (!proxy[Cryptpad.displayNameKey]) {
proxy[Cryptpad.displayNameKey] = uname;
}
Cryptpad.eraseTempSessionValues();
logMeIn(result);
});
});
break;
default: // UNHANDLED ERROR
2017-06-19 12:18:10 +00:00
registering = false;
2017-05-10 13:54:09 +00:00
Cryptpad.errorLoadingScreen(Messages.login_unhandledError);
}
return;
}
2017-06-01 08:23:50 +00:00
if (Test.testing) { return void logMeIn(result); }
2017-05-10 13:54:09 +00:00
Cryptpad.eraseTempSessionValues();
if (shouldImport) {
sessionStorage.migrateAnonDrive = 1;
}
proxy.login_name = uname;
proxy[Cryptpad.displayNameKey] = uname;
sessionStorage.createReadme = 1;
logMeIn(result);
});
}, 0);
}, 200);
2017-03-14 11:00:23 +00:00
}, {
ok: Messages.register_writtenPassword,
cancel: Messages.register_cancel,
2017-03-14 11:00:23 +00:00
cancelClass: 'safe',
okClass: 'danger',
reverseOrder: true,
2017-09-11 14:24:43 +00:00
done: function ($dialog) {
$dialog.find('> div').addClass('half');
},
}, true);
2017-07-04 12:56:03 +00:00
}, 150);
});
var clickRegister = Cryptpad.notAgainForAnother(function () {
$register.click();
}, 500);
$register.on('keypress', function (e) {
e.preventDefault();
e.stopPropagation();
console.error(e.which);
switch (e.which) {
case 13: return clickRegister();
case 13: return clickRegister();
default:
//console.log(e.which);
}
});
2017-06-01 08:23:50 +00:00
Test(function () {
$uname.val('test' + Math.random());
2017-09-13 12:00:48 +00:00
$passwd.val('testtest');
$confirm.val('testtest');
2017-06-01 08:23:50 +00:00
$checkImport[0].checked = true;
$checkAcceptTerms[0].checked = true;
$register.click();
window.setTimeout(function () {
Cryptpad.findOKButton().click();
2017-06-01 08:23:50 +00:00
}, 1000);
});
});
});