text: var readlineSync = require("readline-sync");
  const api = require("./api");
  
  //const phone = "+99966XYYYY";
  var code = "XXXXX";
  
  class AUTH {
    constructor() {}
    async getUser() {
      try {
        const user = await api.call("users.getFullUser", {
          id: {
            _: "inputUserSelf",
          },
        });
  
        return user;
      } catch (error) {
        return null;
      }
    }
  
    sendCode(phone) {
      return api.call("auth.sendCode", {
        phone_number: phone,
        settings: {
          _: "codeSettings",
        },
      });
    }
  
    signIn({ code, phone, phone_code_hash }) {
      return api.call("auth.signIn", {
        phone_code: code,
        phone_number: phone,
        phone_code_hash: phone_code_hash,
      });
    }
  
    signUp({ phone, phone_code_hash }) {
      return api.call("auth.signUp", {
        phone_number: phone,
        phone_code_hash: phone_code_hash,
        first_name: "MTProto",
        last_name: "Core",
      });
    }
  
    getPassword() {
      return api.call("account.getPassword");
    }
  
    checkPassword({ srp_id, A, M1 }) {
      return api.call("auth.checkPassword", {
        password: {
          _: "inputCheckPasswordSRP",
          srp_id,
          A,
          M1,
        },
      });
    }
  
    async auth(phone) {
      const user = await this.getUser();
  
      if (!user) {
        const { phone_code_hash } = await this.sendCode(phone);
  
        // Wait for user's response.
        code = readlineSync.question("[+] Type in your login code: ");
  
        try {
          const signInResult = await this.signIn({
            code,
            phone,
            phone_code_hash,
          });
  
          if (signInResult._ === "auth.authorizationSignUpRequired") {
            await this.signUp({
              phone,
              phone_code_hash,
            });
          }
        } catch (error) {
          if (error.error_message !== "SESSION_PASSWORD_NEEDED") {
            console.log(`error:`, error);
  
            return;
          }
  
          // 2FA
  
          const password = "USER_PASSWORD";
  
          const { srp_id, current_algo, srp_B } = await this.getPassword();
          const { g, p, salt1, salt2 } = current_algo;
  
          const { A, M1 } = await api.mtproto.crypto.getSRPParams({
            g,
            p,
            salt1,
            salt2,
            gB: srp_B,
            password,
          });
  
          const checkPasswordResult = await this.checkPassword({ srp_id, A, M1 });
        }
      }
    }
  }
  
  const auth = new AUTH();
  
  module.exports = auth;
  