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.

152 lines
4.6 KiB

import mediaSizes from '../helpers/mediaSizes';
import { AuthSentCode, AuthSentCodeType, AuthSignIn } from '../layer';
import appStateManager from '../lib/appManagers/appStateManager';
import apiManager from '../lib/mtproto/mtprotoworker';
import Page from './page';
import pageIm from './pageIm';
import pagePassword from './pagePassword';
import pageSignIn from './pageSignIn';
import pageSignUp from './pageSignUp';
4 years ago
import TrackingMonkey from '../components/monkeys/tracking';
import CodeInputField from '../components/codeInputField';
let authCode: AuthSentCode.authSentCode = null;
let headerElement: HTMLHeadElement = null;
let sentTypeElement: HTMLParagraphElement = null;
let codeInput: HTMLInputElement;
let onFirstMount = (): Promise<any> => {
const CODELENGTH = (authCode.type as AuthSentCodeType.authSentCodeTypeApp).length;
const codeInputField = new CodeInputField({
label: 'Code',
name: 'code',
length: CODELENGTH,
onFill: (code) => {
submitCode('' + code);
}
});
codeInput = codeInputField.input as HTMLInputElement;
page.pageEl.querySelector('.input-wrapper').append(codeInputField.container);
const editButton = page.pageEl.querySelector('.phone-edit') as HTMLElement;
4 years ago
editButton.addEventListener('click', function() {
return pageSignIn.mount();
});
4 years ago
const cleanup = () => {
setTimeout(() => {
4 years ago
monkey.remove();
}, 300);
};
4 years ago
const submitCode = (code: string) => {
codeInput.setAttribute('disabled', 'true');
const params: AuthSignIn = {
phone_number: authCode.phone_number,
phone_code_hash: authCode.phone_code_hash,
phone_code: code
};
4 years ago
//console.log('invoking auth.signIn with params:', params);
apiManager.invokeApi('auth.signIn', params, {ignoreErrors: true})
.then((response) => {
4 years ago
//console.log('auth.signIn response:', response);
switch(response._) {
case 'auth.authorization':
apiManager.setUserAuth(response.user.id);
pageIm.mount();
cleanup();
break;
case 'auth.authorizationSignUpRequired':
4 years ago
//console.log('Registration needed!');
pageSignUp.mount({
'phone_number': authCode.phone_number,
'phone_code_hash': authCode.phone_code_hash
});
cleanup();
break;
/* default:
codeInput.innerText = response._;
break; */
}
}).catch(async(err) => {
switch(err.type) {
case 'SESSION_PASSWORD_NEEDED':
4 years ago
//console.warn('pageAuthCode: SESSION_PASSWORD_NEEDED');
err.handled = true;
await pagePassword.mount();
break;
case 'PHONE_CODE_EXPIRED':
codeInput.classList.add('error');
codeInputField.label.innerText = 'Code expired';
break;
case 'PHONE_CODE_EMPTY':
case 'PHONE_CODE_INVALID':
codeInput.classList.add('error');
codeInputField.label.innerText = 'Invalid Code';
break;
default:
codeInputField.label.innerText = err.type;
break;
}
codeInput.removeAttribute('disabled');
});
};
4 years ago
const imageDiv = page.pageEl.querySelector('.auth-image') as HTMLDivElement;
4 years ago
const size = mediaSizes.isMobile ? 100 : 166;
4 years ago
const monkey = new TrackingMonkey(codeInputField, size);
imageDiv.append(monkey.container);
return monkey.load();
};
const page = new Page('page-authCode', true, onFirstMount, (_authCode: typeof authCode) => {
authCode = _authCode;
if(!headerElement) {
headerElement = page.pageEl.getElementsByClassName('phone')[0] as HTMLHeadElement;
sentTypeElement = page.pageEl.getElementsByClassName('sent-type')[0] as HTMLParagraphElement;
} else {
codeInput.value = '';
const evt = document.createEvent('HTMLEvents');
evt.initEvent('input', false, true);
codeInput.dispatchEvent(evt);
}
headerElement.innerText = authCode.phone_number;
switch(authCode.type._) {
case 'auth.sentCodeTypeSms':
sentTypeElement.innerHTML = 'We have sent you an SMS<br>with the code.';
break;
case 'auth.sentCodeTypeApp':
sentTypeElement.innerHTML = 'We have sent you a message in Telegram<br>with the code.';
break;
case 'auth.sentCodeTypeCall':
sentTypeElement.innerHTML = 'We will call you and voice<br>the code.';
break;
default:
sentTypeElement.innerHTML = `Please check everything<br>for a code (type: ${authCode.type._})`;
break;
}
appStateManager.pushToState('authState', {_: 'authStateAuthCode', sentCode: _authCode});
appStateManager.saveState();
}, () => {
codeInput.focus();
});
export default page;