Giter VIP home page Giter VIP logo

Comments (10)

 avatar commented on July 2, 2024

You may use Auth.prototype.getState for checking authorization state:

const { auth, updates } = airgram
airgram.use(auth)
airgram.use(updates)

const getDialogs = () => airgram.client.messages.getDialogs({/* ... */})

auth.getState().then(({ userId }) => {
  if (userId) {
    return getDialogs()
  }
  return auth.login().then(getDialogs)
}).catch((error) => {
  console.error(error)
})

from airgram.

amir3code avatar amir3code commented on July 2, 2024

Well I get what you say, but there is one problem and I have many problems with asynchronous codes 😆 I dont know if it is only my problem but they seem very uncontrollable.
Can you show me where to put this code in the index.ts file of your airgram-ts-example?

I have problem with that auth.login thing cause I have used it before in the index.ts file and I think it caused a double login and made the old stored keys to disappear and I should have removed the database and login again to make the system work again.
Can I ommit the auth.login part? I mean can we assume that we are logged in by now and then get the state and check its value?

from airgram.

esindger avatar esindger commented on July 2, 2024

Okay, I will update the example today.

from airgram.

amir3code avatar amir3code commented on July 2, 2024

Thanks for the response!
I have another question, How this part of code in the index.ts file know that we are connected to telegram servers and logged in. How does it tell? My question is about the whole asynchronous thing.

// Get dialogs list
airgram.client.messages.getDialogs({
  flags: 0,
  limit: 30,
  offset_date: 0,
  offset_id: 0,
  offset_peer: { _: 'inputPeerEmpty' }
}).then((dialogs) => {
  console.info(dialogs)
}).catch((error) => {
  console.error(error)
})

from airgram.

esindger avatar esindger commented on July 2, 2024

Can I ommit the auth.login part? I mean can we assume that we are logged in by now and then get the state and check its value?

If you use Auth middleware you don't should not to call auth.login() because middleware checks whether you are authorized or not (you won't be signed in twice).

Method auth.login() unregister your previous access keys, so you need to check the current state like:

const { userId } = await auth.getState()

If userId is defined just skip the login step.

I think it's good idea to create auth.onLogin(): Promise<ag.AuthDoc> method:

auth.onLogin().then(({ userId }) => {
  // your own code
})

from airgram.

amir3code avatar amir3code commented on July 2, 2024

Yeah yeah that would be perfect cause when I looked at the index.ts file I thought to myself that this is whole asynchronous and how can I guarantee that what I want gets executed when I am surely logged in. Thanks for your time!

from airgram.

esindger avatar esindger commented on July 2, 2024

I have another question, How this part of code in the index.ts file know that we are connected to telegram servers and logged in. How does it tell? My question is about the whole asynchronous thing.

Method airgram.client.messages.getDialogs() knows nothing about authorization status. The Auth middleware checks authorization status and calls auth.login() if it is necessary.

from airgram.

amir3code avatar amir3code commented on July 2, 2024

Method airgram.client.messages.getDialogs() knows nothing about authorization status. The Auth middleware checks authorization status and calls auth.login() if it is necessary.

Aha so you mean If I am not logged in, It returns an error, right? So it assumes that we are logged in and it is not fault-tolerant.

from airgram.

 avatar commented on July 2, 2024

Please wait for update. This behavior will be changed.

from airgram.

 avatar commented on July 2, 2024

Now you can freely use auth.login(). Authorization keys wont be deleted. If user is already signed in, authorization will be skipped.

auth.login().then(async () => {
  // Start long polling
  await updates.startPolling()

  // Get dialogs list
  const dialogs = await airgram.client.messages.getDialogs({
    flags: 0,
    limit: 30,
    offset_date: 0,
    offset_id: 0,
    offset_peer: { _: 'inputPeerEmpty' }
  })

  console.log(dialogs)

}).catch((error) => {
  console.error(error)
})

Full example is here.

from airgram.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.