gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.23k stars 176 forks source link

I recived Error "PHONE_CODE_EXPIRED" when send code and signin. #655

Open giangnv2 opened 5 months ago

giangnv2 commented 5 months ago

My code:

const session = new StringSession('');
const client = new TelegramClient(session, apiId, apiHash, {});
const getPhoneHash = async () => {
    try {
        await client.connect();
    } catch (error) {
        console.error('Error:', error);
    }  
    const {phoneCodeHash, isCodeViaApp} = await client.sendCode({
            apiId: apiId,
            apiHash: apiHash,
    }, phone);
    setPhoneHash(phoneCodeHash)
    console.log(phoneCodeHash, isCodeViaApp)
}
const confirmLogin = async () => {
    try {
        await client.connect();
    } catch (error) {
        console.error('Error:', error);
    }  
    console.log(phoneCode, phoneHash, phone)
    const result = await client.invoke(
        new Api.auth.SignIn({
          phoneNumber: phone,
          phoneCodeHash: phoneHash,
          phoneCode: phoneCode,
        })
      );
    console.log(result); // prints the result
}
alvvaysxxx commented 5 months ago

that's because you should send code and sign in in one session, but you are using 2 client.connect() so it means that its 2 different sessions. If you need more clarification then reply me