zerobias / telegram-mtproto

Telegram client api (MTProto) library
MIT License
620 stars 136 forks source link

Logging in for the second time #137

Closed eboblin closed 6 years ago

eboblin commented 6 years ago

Ok, after some struggle i've logged in. Thanks for mentioning that auth.signIn and auth.sendCode should be within one object instance. How should i login all other times without entering sms codes again and again? I receive id and access_hash in user.auth object, and probably it is something valid, but when i try to put them into auth.importAuthorization, it returns AUTH_BYTES_INVALID. Probably this function auth.importAuthorization is needed for something else and my thought is wrong. Please advice the right flow for this situation. Thanks.

zerobias commented 6 years ago

You don't need auth.importAuthorization in that case (it's for migration between dc's)

Library uses storage adapters to save authorization. storage-fs for nodejs and storage-browser for browsers. They work in the background and will save necessary info right after your authorization. Every library instance connected to the same adapter will be the same.

The library has separate test to ensure that we can use previously saved authorization

const { Storage } = require('mtproto-storage-fs')
const storagePath = './storage.json'

const firstInstance = MTProto({
  app: {
    storage: new Storage(storagePath),
  }
})

// ... auth, sms code, exit

const secondInstance = MTProto({
  app: {
    storage: new Storage(storagePath), // New instance but the same file
  }
})

await secondInstance('help.getNearestDc')
await secondInstance('messages.getDialogs', {
  limit: 100,
})
// Typical requests; checks that everything works as expected
zerobias commented 6 years ago

Added it to F.A.Q. https://github.com/zerobias/telegram-mtproto/wiki/FAQ#how-could-i-login-without-entering-sms-codes-every-time