Skip to main content

User Authorization

Requirements#

Methods#

getUser#

async function getUser() {
try {
const user = await api.call('users.getFullUser', {
id: {
_: 'inputUserSelf',
},
});
return user;
} catch (error) {
return null;
}
}

sendCode#

function sendCode(phone) {
return api.call('auth.sendCode', {
phone_number: phone,
settings: {
_: 'codeSettings',
},
});
}

signIn#

function signIn({ code, phone, phone_code_hash }) {
return api.call('auth.signIn', {
phone_code: code,
phone_number: phone,
phone_code_hash: phone_code_hash,
});
}

signUp#

function 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#

function getPassword() {
return api.call('account.getPassword');
}

checkPassword#

function checkPassword({ srp_id, A, M1 }) {
return api.call('auth.checkPassword', {
password: {
_: 'inputCheckPasswordSRP',
srp_id,
A,
M1,
},
});
}

Flow#

TODO

Code#

I recommend using test phone numbers during development

(async () => {
const user = await getUser();
const phone = '+99966XYYYY';
const code = 'XXXXX';
if (!user) {
const { phone_code_hash } = await sendCode(phone);
try {
const signInResult = await signIn({
code,
phone,
phone_code_hash,
});
if (signInResult._ === 'auth.authorizationSignUpRequired') {
await 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 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 checkPassword({ srp_id, A, M1 });
}
}
})();