Files
NodeBB/public/src/client/login.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

"use strict";
/* global define, app, config, RELATIVE_PATH */
define('forum/login', ['translator'], function (translator) {
var Login = {};
Login.init = function () {
var errorEl = $('#login-error-notify'),
submitEl = $('#login'),
formEl = $('#login-form');
submitEl.on('click', function (e) {
e.preventDefault();
if (!$('#username').val() || !$('#password').val()) {
2015-04-07 11:44:45 -04:00
errorEl.find('p').translateText('[[error:invalid-username-or-password]]');
errorEl.show();
} else {
errorEl.hide();
2015-04-07 11:44:45 -04:00
if (submitEl.hasClass('disabled')) {
return;
}
2015-04-07 11:44:45 -04:00
submitEl.addClass('disabled');
/*
Set session refresh flag (otherwise the session check will trip and throw invalid session modal)
We know the session is/will be invalid (uid mismatch) because the user is attempting a login
*/
app.flags = app.flags || {};
app.flags._sessionRefresh = true;
2015-04-07 11:44:45 -04:00
formEl.ajaxSubmit({
headers: {
2017-02-17 19:31:21 -07:00
'x-csrf-token': config.csrf_token,
2015-04-07 11:44:45 -04:00
},
success: function (data, status) {
2015-09-18 10:58:32 -04:00
window.location.href = data + '?loggedin';
2015-04-07 11:44:45 -04:00
},
error: function (data, status) {
if (data.status === 403 && data.responseText === 'Forbidden') {
2016-05-09 11:40:42 -04:00
window.location.href = config.relative_path + '/login?error=csrf-invalid';
} else {
errorEl.find('p').translateText(data.responseText);
errorEl.show();
submitEl.removeClass('disabled');
app.flags._sessionRefresh = false;
2016-08-02 17:48:28 -04:00
// Select the entire password if that field has focus
if ($('#password:focus').size()) {
$('#password').select();
}
2016-05-09 11:40:42 -04:00
}
2017-02-17 19:31:21 -07:00
},
2015-04-07 11:44:45 -04:00
});
}
});
$('#login-error-notify button').on('click', function (e) {
e.preventDefault();
errorEl.hide();
2015-04-07 11:44:45 -04:00
return false;
});
2016-05-03 19:13:10 +03:00
if ($('#content #username').attr('readonly')) {
2016-05-10 18:34:41 +03:00
$('#content #password').val('').focus();
2016-05-03 19:13:10 +03:00
} else {
$('#content #username').focus();
}
};
return Login;
});