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

57 lines
1.5 KiB
TypeScript
Raw Normal View History

import { computeSrpParams } from '@mtcute/core/utils'
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._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)
}