2023-09-21 13:18:09 +03:00
|
|
|
import { 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'
|
|
|
|
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,
|
2023-06-05 00:30:48 +00:00
|
|
|
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',
|
|
|
|
}),
|
2023-06-05 00:30:48 +00:00
|
|
|
password,
|
2021-04-08 12:19:38 +03:00
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
assertTypeIs(
|
|
|
|
'checkPassword (@ auth.checkPassword)',
|
|
|
|
res,
|
2023-06-05 00:30:48 +00:00
|
|
|
'auth.authorization',
|
2021-04-08 12:19:38 +03:00
|
|
|
)
|
|
|
|
assertTypeIs(
|
|
|
|
'checkPassword (@ auth.checkPassword -> user)',
|
|
|
|
res.user,
|
2023-06-05 00:30:48 +00:00
|
|
|
'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)
|
|
|
|
}
|