tdlib-native
Version:
🚀 Telegram TDLib native nodejs wrapper
254 lines (253 loc) • 6.39 kB
JavaScript
import { TDError } from "./client.mjs";
import { AuthorizationState$Type, typename, Update$Type } from "./generated/types.mjs";
async function wait(waiter, ...parameters) {
if (typeof waiter === "function") {
return await waiter(...parameters);
}
return waiter;
}
class Authenticator {
/**
*
*
* @static
* @param {Client} client
* @returns {StageTDLibParameters} {StageTDLibParameters}
* @memberof Authenticator
*/
static create(client) {
return new Authenticator(client);
}
_client;
_state = {};
/**
* Creates an instance of Authenticator.
* @param {Client} client
* @memberof Authenticator
*/
constructor(client) {
this._client = client;
}
/**
*
*
* @private
* @param {AuthorizationState} state
* @param {function(): void} resolve
* @returns {Promise<void>} {Promise<void>}
* @memberof Authenticator
*/
async _handleUpdate(state, resolve) {
switch (state._) {
case AuthorizationState$Type.Ready: {
resolve();
return;
}
case AuthorizationState$Type.Closed:
case AuthorizationState$Type.Closing:
case AuthorizationState$Type.LoggingOut: {
throw new TDError("Closing");
}
case AuthorizationState$Type.WaitCode: {
if (!this._state.code) {
return;
}
await this._client.api.checkAuthenticationCode({
code: await wait(this._state.code, state.code_info)
});
return;
}
case AuthorizationState$Type.WaitEmailAddress: {
if (!this._state.email) {
return;
}
await this._client.api.setAuthenticationEmailAddress({
email_address: await wait(this._state.email)
});
return;
}
case AuthorizationState$Type.WaitEmailCode: {
if (!this._state.emailCode) {
return;
}
await this._client.api.checkAuthenticationEmailCode({
code: {
[typename]: "emailAddressAuthenticationCode",
code: await wait(this._state.emailCode, state.code_info)
}
});
return;
}
case AuthorizationState$Type.WaitOtherDeviceConfirmation: {
if (!this._state.otherDevice) {
return;
}
await wait(this._state.otherDevice, state.link);
return;
}
case AuthorizationState$Type.WaitPassword: {
if (!this._state.password) {
return;
}
await this._client.api.checkAuthenticationPassword({
password: await wait(this._state.password, state.password_hint)
});
return;
}
case AuthorizationState$Type.WaitPhoneNumber: {
if (this._state.token) {
await this._client.api.checkAuthenticationBotToken({
token: await wait(this._state.token)
});
return;
}
if (this._state.phone) {
await this._client.api.setAuthenticationPhoneNumber({
phone_number: await wait(this._state.phone),
settings: this._state.phoneSettings ? await wait(this._state.phoneSettings) : void 0
});
}
return;
}
case AuthorizationState$Type.WaitTdlibParameters: {
if (!this._state.parameters) {
return;
}
await this._client.api.setTdlibParameters(
await wait(this._state.parameters)
);
return;
}
case AuthorizationState$Type.WaitRegistration: {
if (!this._state.register) {
return;
}
await this._client.api.registerUser(
await wait(this._state.register, state.terms_of_service)
);
return;
}
}
}
/**
*
*
* @param {AuthenticateOptions} [options]
* @returns {Promise<void>} {Promise<void>}
* @throws {TDError} if underlying method call throws
* @throws {Error} if authentication state changes to closed before authentication completes
* @throws {Error} whatever is reason of cancellation token
* @memberof Authenticator
*/
async authenticate(options) {
let unsubscribe;
const promise = new Promise((resolve, reject) => {
var _a;
unsubscribe = this._client.updates.subscribe((update) => {
if (update._ !== Update$Type.AuthorizationState) {
return;
}
this._handleUpdate(update.authorization_state, resolve).catch(reject);
});
(_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.addEventListener("abort", reject, { once: true });
});
try {
return await promise;
} finally {
unsubscribe == null ? void 0 : unsubscribe();
}
}
/**
*
*
* @param {*} parameters
* @returns {StageSelect} {StageSelect}
* @memberof Authenticator
*/
tdlibParameters(parameters) {
this._state.parameters = parameters;
return this;
}
/**
*
*
* @param {Waiter<string>} token
* @returns {StageSelect} {StageSelect}
* @memberof Authenticator
*/
token(token) {
this._state.token = token;
return this;
}
/**
*
*
* @param {Waiter<string>} phone
* @param {*} settings
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
phone(phone, settings) {
this._state.phone = phone;
this._state.phoneSettings = settings;
return this;
}
/**
*
*
* @param {Waiter<string>} email
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
email(email) {
this._state.email = email;
return this;
}
/**
*
*
* @param {Waiter<string>} code
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
code(code) {
this._state.code = code;
return this;
}
/**
*
*
* @param {Waiter<string>} emailCode
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
emailCode(emailCode) {
this._state.emailCode = emailCode;
return this;
}
/**
*
*
* @param {Waiter<string>} password
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
password(password) {
this._state.password = password;
return this;
}
/**
*
*
* @param {*} data
* @returns {StageUser} {StageUser}
* @memberof Authenticator
*/
register(data) {
this._state.register = data;
return this;
}
}
export {
Authenticator
};