Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.1 KiB

/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import { putPreloader } from '../components/misc';
import mediaSizes from '../helpers/mediaSizes';
import { AccountPassword } from '../layer';
import appStateManager from '../lib/appManagers/appStateManager';
import passwordManager from '../lib/mtproto/passwordManager';
import Page from './page';
import pageIm from './pageIm';
import Button from '../components/button';
4 years ago
import PasswordInputField from '../components/passwordInputField';
import PasswordMonkey from '../components/monkeys/password';
import RichTextProcessor from '../lib/richtextprocessor';
import I18n from '../lib/langPack';
import LoginPage from './loginPage';
import { cancelEvent } from '../helpers/dom/cancelEvent';
import { attachClickEvent } from '../helpers/dom/clickEvent';
import htmlToSpan from '../helpers/dom/htmlToSpan';
import replaceContent from '../helpers/dom/replaceContent';
import toggleDisability from '../helpers/dom/toggleDisability';
const TEST = false;
let passwordInput: HTMLInputElement;
let onFirstMount = (): Promise<any> => {
const page = new LoginPage({
className: 'page-password',
withInputWrapper: true,
titleLangKey: 'Login.Password.Title',
subtitleLangKey: 'Login.Password.Subtitle'
});
const btnNext = Button('btn-primary btn-color-primary');
const btnNextI18n = new I18n.IntlElement({key: 'Login.Next'});
btnNext.append(btnNextI18n.element);
4 years ago
const passwordInputField = new PasswordInputField({
label: 'LoginPassword',
4 years ago
name: 'password'
});
passwordInput = passwordInputField.input as HTMLInputElement;
page.inputWrapper.append(passwordInputField.container, btnNext);
let getStateInterval: number;
let getState = () => {
// * just to check session relevance
if(!getStateInterval) {
getStateInterval = window.setInterval(getState, 10e3);
}
return !TEST && passwordManager.getState().then(_state => {
state = _state;
if(state.hint) {
replaceContent(passwordInputField.label, htmlToSpan(RichTextProcessor.wrapEmojiText(state.hint)));
} else {
passwordInputField.setLabel();
}
});
};
let state: AccountPassword;
const onSubmit = (e?: Event) => {
if(e) {
cancelEvent(e);
}
if(!passwordInput.value.length) {
passwordInput.classList.add('error');
return;
}
const toggle = toggleDisability([passwordInput, btnNext], true);
let value = passwordInput.value;
btnNextI18n.update({key: 'PleaseWait'});
const preloader = putPreloader(btnNext);
passwordManager.check(value, state).then((response) => {
4 years ago
//console.log('passwordManager response:', response);
switch(response._) {
case 'auth.authorization':
clearInterval(getStateInterval);
pageIm.mount();
4 years ago
if(monkey) monkey.remove();
break;
default:
btnNext.removeAttribute('disabled');
btnNextI18n.update({key: response._ as any});
preloader.remove();
break;
}
}).catch((err: any) => {
toggle();
passwordInputField.input.classList.add('error');
switch(err.type) {
default:
//btnNext.innerText = err.type;
btnNextI18n.update({key: 'PASSWORD_HASH_INVALID'});
passwordInput.select();
break;
}
preloader.remove();
getState();
});
};
attachClickEvent(btnNext, onSubmit);
passwordInput.addEventListener('keypress', function(this, e) {
this.classList.remove('error');
btnNextI18n.update({key: 'Login.Next'});
if(e.key === 'Enter') {
return onSubmit();
}
});
4 years ago
const size = mediaSizes.isMobile ? 100 : 166;
4 years ago
const monkey = new PasswordMonkey(passwordInputField, size);
page.imageDiv.append(monkey.container);
return Promise.all([
4 years ago
monkey.load(),
getState()
]);
};
const page = new Page('page-password', true, onFirstMount, null, () => {
//if(!isAppleMobile) {
passwordInput.focus();
//}
appStateManager.pushToState('authState', {_: 'authStatePassword'});
});
export default page;