Skip to content

Commit

Permalink
fix "null is not an object" in client.login()
Browse files Browse the repository at this point in the history
Resolves #180
  • Loading branch information
eilvelia committed Dec 20, 2024
1 parent 3aee94f commit c3afc5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

<!-- Hi! -->

## [email protected] (UNRELEASED)

- Fixed a possible `TypeError: null is not an object` error in `client.login()`
if called from a database with an incomplete login attempt.

## [email protected] (2024-07-19)

- No longer generates `number | string` instead of `string` for `int64` types
Expand Down
34 changes: 17 additions & 17 deletions packages/tdl/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,21 @@ export class Client {
return new Promise((resolve, reject) => {
if (this._client.val == null)
return reject(new Error('The client is closed'))
let loginDetails: StrictLoginDetails | null = null
function needLoginDetails (l: StrictLoginDetails | null): asserts l is StrictLoginDetails {
if (l == null && loginDetails == null) {
loginDetails = mergeDeepRight(
let cachedLoginDetails: StrictLoginDetails | null = null
function needLoginDetails (): StrictLoginDetails {
if (cachedLoginDetails == null) {
cachedLoginDetails = mergeDeepRight(
defaultLoginDetails,
typeof arg === 'function' ? arg() : arg
)
l = loginDetails
) as StrictLoginDetails
}
if (l == null) throw new Error('Invariant violation: loginDetails is null')
return cachedLoginDetails
}
function needUserLogin (l: StrictLoginDetails | null): asserts l is LoginUser {
if (l == null) needLoginDetails(l)
if (l.type !== 'user')
function needUserLogin (): LoginUser {
const loginDetails = needLoginDetails()
if (loginDetails.type !== 'user')
throw new Error('Expected to log in as a bot, received user auth update')
return loginDetails
}
const processAuthorizationState = async (authState: Td.AuthorizationState) => {
// Note: authorizationStateWaitPhoneNumber may not be the first update
Expand All @@ -579,7 +579,7 @@ export class Client {
}

case 'authorizationStateWaitPhoneNumber': {
needLoginDetails(loginDetails)
const loginDetails = needLoginDetails()
let retry = false
if (loginDetails.type === 'user') {
while (true) {
Expand Down Expand Up @@ -614,7 +614,7 @@ export class Client {

// TDLib >= v1.8.6 only
case 'authorizationStateWaitEmailAddress': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
await this.invoke({
_: 'setAuthenticationEmailAddress',
email_address: await loginDetails.getEmailAddress()
Expand All @@ -624,7 +624,7 @@ export class Client {

// TDLib >= v1.8.6 only
case 'authorizationStateWaitEmailCode': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
await this.invoke({
_: 'checkAuthenticationEmailCode',
code: {
Expand All @@ -637,13 +637,13 @@ export class Client {
}

case 'authorizationStateWaitOtherDeviceConfirmation': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
loginDetails.confirmOnAnotherDevice(authState.link)
return
}

case 'authorizationStateWaitCode': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
let retry = false
while (true) {
const code = await loginDetails.getAuthCode(retry)
Expand All @@ -663,7 +663,7 @@ export class Client {
}

case 'authorizationStateWaitRegistration': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
const { firstName, lastName = '' } = await loginDetails.getName()
await this.invoke({
_: 'registerUser',
Expand All @@ -674,7 +674,7 @@ export class Client {
}

case 'authorizationStateWaitPassword': {
needUserLogin(loginDetails)
const loginDetails = needUserLogin()
const passwordHint = authState.password_hint
let retry = false
while (true) {
Expand Down

0 comments on commit c3afc5c

Please sign in to comment.