mtcute/packages/client/src/methods/auth/check-password.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

import { computeSrpParams } from '@mtcute/core'
2021-04-08 12:19:38 +03:00
import { TelegramClient } from '../../client'
import { User } from '../../types'
import { assertTypeIs } from '../../utils/type-assertion'
/**
* Check your Two-Step verification password and log in
*
* @param password Your Two-Step verification password
* @returns The authorized user
* @throws BadRequestError In case the password is invalid
* @internal
*/
export async function checkPassword(
this: TelegramClient,
password: string,
2021-04-08 12:19:38 +03:00
): Promise<User> {
const res = await this.call({
_: 'auth.checkPassword',
password: await computeSrpParams(
this._crypto,
await this.call({
_: 'account.getPassword',
}),
password,
2021-04-08 12:19:38 +03:00
),
})
assertTypeIs(
'checkPassword (@ auth.checkPassword)',
res,
'auth.authorization',
2021-04-08 12:19:38 +03:00
)
assertTypeIs(
'checkPassword (@ auth.checkPassword -> user)',
res.user,
'user',
2021-04-08 12:19:38 +03:00
)
this.log.prefix = `[USER ${this._userId}] `
this._userId = res.user.id
this._isBot = false
this._selfChanged = true
this._selfUsername = res.user.username ?? null
await this._fetchUpdatesState()
await this._saveStorage()
2021-04-08 12:19:38 +03:00
// telegram ignores invokeWithoutUpdates for auth methods
2023-06-10 00:37:26 +03:00
// todo where is this._disableUpdates?
// if (this._disableUpdates) this.primaryConnection._resetSession()
// else
this.startUpdatesLoop()
2021-04-08 12:19:38 +03:00
return new User(this, res.user)
}