window.lang = "ru-RU";
window.theme = "dark";
window.openId = {};
window.openId.enable = ('true' == 'true');
window.openId.issuerName = "Trusted.ID";
(function ($) {
let theme = window.theme || 'dark';
let lang = window.lang || 'ru-RU';
setTheme();
initLocal(lang);
initLocale(lang);
const urlParams = new URLSearchParams(window.location.search),
newUserParam = urlParams.get('new'),
noRoleParam = urlParams.get('no_role'),
messageParam = urlParams.get('message'),
triesParam = urlParams.get('tries'),
timeoutParam = urlParams.get('timeout');
if (newUserParam == 'true') newUser();
if (noRoleParam == 'true') noRole();
if (messageParam == 'login_fail') loginFail(triesParam);
if (messageParam == 'no_tries') noTries(timeoutParam);
window.history.replaceState(null, null, window.location.pathname);
function setTheme() {
if (theme == 'dark') {
$('.navbar').addClass('navbar-dark');
$('.navbar').removeClass('navbar-light');
$('body').addClass('bg-dark');
$('body').removeClass('bg-light');
} else if (theme == 'light') {
$('.navbar').removeClass('navbar-dark');
$('.navbar').addClass('navbar-light');
$('body').removeClass('bg-dark');
$('body').addClass('bg-light');
}
}
function initLocale(locale) {
$('html').attr('lang', locale.substring(0, 2));
$('#locale > [value=' + locale + ']').attr('selected', true);
i18next.changeLanguage(locale.substring(0, 2));
$('body').localize();
}
$(document).keypress((e) => {
let keycode = event.keyCode ? event.keyCode : event.which;
if (keycode == '13') {
$('#loginButton').click();
}
});
function loginFail(tries) {
let msg =
i18next.t('auth.loginFailed') +
'
' +
i18next.t('auth.loginTriesLeft', {
tries: tries
});
$('#error').html(msg);
$('#error').show();
}
function noTries(timeout) {
let msg =
i18next.t('auth.loginFailedNoTries') +
'
' +
i18next.t('auth.loginNextTry', {
timeout: new Date(
timeout.toLocaleString(lang, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
})
).toLocaleString()
});
$('#error').html(msg);
$('#error').show();
}
function noRole() {
let msg = i18next.t('auth.noRole');
$('#error').html(msg);
$('#error').show();
}
function newUser() {
let msg = i18next.t('auth.successReg');
$('#error').html(msg);
$('#error').show();
}
$('#loginButton').on('click', () => {
let params = {
username: $('[name="username"]', '#login-form').val().trim(),
password: ''
};
if ($('[name="password"]', '#login-form').val().trim().length > 0) {
params.password = $('[name="password"]', '#login-form').val().trim();
}
$.ajax({
url: '/api/v1/Login/Basic',
method: 'POST',
data: JSON.stringify(params),
contentType: 'application/json',
success: (json) => {
if (json && json.success) window.location.href = '/';
else if (json && !json.success && json.new) newUser();
},
error: (err) => {
let json = err.responseJSON;
if (!json.success && json.message == 'login_fail') {
loginFail(json.tries);
} else if (!json.success && json.message == 'no_tries') {
noTries(json.timeout);
} else if (!json.success && json.message == 'no_role') {
noRole();
}
}
});
return false;
});
if (window.openId?.enable) {
const issuerNameUndefined =
window.openId?.issuerName == 'undefined' || !window.openId?.issuerName;
$('#enterExternalAppButton').html(
(window.openId?.issuerName?.includes('Trusted') || issuerNameUndefined
? "
"
: '') +
i18next.t('buttons.thirdPartyAuth') +
(issuerNameUndefined ? 'Trusted.ID' : window.openId.issuerName)
);
$('#enterExternalAppButton').show();
}
$('#enterExternalAppButton').on('click', () => {
let url = new URL('/login', window.location.href).href;
window.location.href = url + '?openid=true';
});
})(jQuery);