2023-09-22 15:32:28 +03:00
|
|
|
import { assertTypeIs, computeSrpParams } from '@mtcute/core/utils'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2021-04-08 12:19:38 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import { User } from '../../types'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-09-24 01:32:22 +03:00
|
|
|
export async function checkPassword(this: TelegramClient, password: string): Promise<User> {
|
2021-04-08 12:19:38 +03:00
|
|
|
const res = await this.call({
|
|
|
|
_: 'auth.checkPassword',
|
|
|
|
password: await computeSrpParams(
|
|
|
|
this._crypto,
|
|
|
|
await this.call({
|
|
|
|
_: 'account.getPassword',
|
|
|
|
}),
|
2023-06-05 00:30:48 +00:00
|
|
|
password,
|
2021-04-08 12:19:38 +03:00
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2023-09-24 01:32:22 +03:00
|
|
|
assertTypeIs('checkPassword (@ auth.checkPassword)', res, 'auth.authorization')
|
|
|
|
assertTypeIs('checkPassword (@ auth.checkPassword -> user)', res.user, 'user')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
2021-04-18 16:23:25 +03:00
|
|
|
this._userId = res.user.id
|
2023-08-12 22:40:37 +03:00
|
|
|
this.log.prefix = `[USER ${this._userId}] `
|
2021-04-18 16:23:25 +03:00
|
|
|
this._isBot = false
|
2021-05-22 14:41:11 +03:00
|
|
|
this._selfChanged = true
|
2021-11-23 00:03:59 +03:00
|
|
|
this._selfUsername = res.user.username ?? null
|
2023-08-12 22:40:37 +03:00
|
|
|
await this.network.notifyLoggedIn(res)
|
|
|
|
|
2021-04-18 16:23:25 +03:00
|
|
|
await this._fetchUpdatesState()
|
2021-04-17 19:00:46 +03:00
|
|
|
await this._saveStorage()
|
2021-04-08 12:19:38 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
// telegram ignores invokeWithoutUpdates for auth methods
|
2023-08-12 22:40:37 +03:00
|
|
|
if (this.network.params.disableUpdates) this.network.resetSessions()
|
|
|
|
else this.startUpdatesLoop()
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2021-04-08 12:19:38 +03:00
|
|
|
return new User(this, res.user)
|
|
|
|
}
|