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

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-09-22 15:32:28 +03:00
import { assertTypeIs, computeSrpParams } from '@mtcute/core/utils'
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',
}),
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
this._userId = res.user.id
2023-08-12 22:40:37 +03:00
this.log.prefix = `[USER ${this._userId}] `
this._isBot = false
this._selfChanged = true
this._selfUsername = res.user.username ?? null
2023-08-12 22:40:37 +03:00
await this.network.notifyLoggedIn(res)
await this._fetchUpdatesState()
await this._saveStorage()
2021-04-08 12:19:38 +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-04-08 12:19:38 +03:00
return new User(this, res.user)
}