Skip to content

TypeError: Bytes or str expected, not Buffer during client.start authentication (v2.26.21, Browser/Next.js) #789

Open
@artem-lazarev

Description

@artem-lazarev

Hi gramJS team,

I'm encountering a TypeError: Bytes or str expected, not Buffer when attempting to log in using the client.start method in a browser environment.

Version Info:

  • gramJS (telegram package): 2.26.21
  • Session: StringSession

Environment:

  • Runtime: Browser (within a Next.js application)
  • Bundler: Next.js internal (Webpack/Turbopack)
  • (Optional: Node.js version used for building: [Your Node.js Version, e.g., v18.18.0])

Problem Description:

After successfully connecting (client.connect()) and requesting a phone code (client.sendCode()), the subsequent call to client.start() fails with the Bytes or str expected, not Buffer error. This occurs specifically during the phase where gramJS likely handles the password/2FA step internally (signInWithPassword appears in the stack trace).

Steps to Reproduce (Conceptual):

  1. Initialize TelegramClient with apiId, apiHash, and an empty StringSession in a browser environment.
  2. Call await client.connect();
  3. Call await client.sendCode({ apiId, apiHash }, phoneNumber);
  4. Prompt the user for the received phone code and optionally their 2FA password.
  5. Call await client.start({ phoneNumber: ..., phoneCode: async () => code, password: async () => password, onError: ... });

Relevant Code Snippet:

This is how client.start is being called within my Zustand store:

// Helper function to provide auth parameters
const createAuthParamCallback = <T extends string>(param: T): () => Promise<T> => {
  return async () => {
    return param;
  };
};

// Inside an async function handling login:
async function loginWithCodePassword({ phoneCode, password = '' }) {
  // ... retrieve client, apiId, apiHash, phoneNumber from state ...

  console.log('[TG Store] Attempting to login with code/password...');
  // Set loading state, etc.

  try {
    console.log('[TG Store] Calling client.start...');
    await client.start({
      phoneNumber: phoneNumber, // String from state
      password: createAuthParamCallback(password), // Always a string (defaulted to '')
      phoneCode: createAuthParamCallback(phoneCode), // String from user input
      onError: (err: Error) => {
        console.error('[TG Store] client.start onError callback triggered:', err);
        // Propagate or handle error
        throw err;
      },
    });
    // ... handle success ...
  } catch (error: any) {
    // ... handle failure ...
     console.error('[TG Store] Login failed during client.start:', error); // <-- This logs the error
  }
}

Error Log & Stack Trace:

[TG Store] Calling client.start...
[TG Store] client.start onError callback triggered: Error: Bytes or str expected, not Buffer
    at serializeBytes (generationHelpers.js:247:19)
    at argToBytes (api.js:106:20)
    at VirtualClass.getBytes (api.js:415:42)
    at argToBytes (api.js:113:22)
    at VirtualClass.getBytes (api.js:415:42)
    at new RequestState (RequestState.js:13:29)
    at Object.invoke (users.js:48:19)
    at async Object.signInWithPassword (auth.js:319:31)
    at async _authFlow (auth.js:363:11)
    at async Object.start (auth.js:52:5)
    at async loginWithCodePassword (telegramStore.ts:220:7) // <-- My code calling client.start
    at async handleCodePasswordSubmit (page.tsx:57:5)     // <-- UI handler calling loginWithCodePassword

(The same error/stack trace is caught by the outer try/catch block)

Expected Behavior:

client.start() should resolve successfully, completing the login process and allowing the session to be saved.

Actual Behavior:

client.start() throws the TypeError: Bytes or str expected, not Buffer, preventing login. The error originates deep within the gramJS library's internal serialization or authentication logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions