alik0211 / mtproto-core

Telegram API JS (MTProto) client library for Node.js and browser
https://mtproto-core.js.org
GNU General Public License v3.0
630 stars 113 forks source link

Handling multiple users in the backend - Question #222

Closed TheRyan1 closed 2 years ago

TheRyan1 commented 2 years ago

When init MTproto in the backend, the session information used is for a single user. Is there a way that multiple users would be able to use a single instance? Or would each user need a new instance with their own session data?

What I want to do: Login from the front end and call the methods from the backend

Context:

refaldyrk commented 1 year ago

how this close, i have same question

refaldyrk commented 1 year ago

what a answer for this question, i need pls

TheRyan1 commented 1 year ago

how this close, i have same question

User's logged in with an email and password. Then connected their account using their phone number. Once authenticated, I stored the session generated by auth.signIn method into the db. So each time they made a call to the mtproto methods, it would use the stored session.

  try {
        const { session } = await User.findById(req.id);
        const parsedSession = JSON.parse(session);
        if (Object.keys(parsedSession).length > 3) {
          let tempStorage = new Storage();
          for (let [key, value] of Object.entries(parsedSession)) {
            tempStorage.set(key, value);
          }
          const _MTproto = new MTprotoAPI(tempStorage);
          const user = await _MTproto.getUser();
          if (user.user) {
            res.send({ user: user.user, step: "dashboard" });
          } else {
            res.send("An error occured");
          }
        } else {
          res.send({ message: "Sign in required", step: "enterPhone" });
        }
      } catch (err) {
        console.log(err);
      }
refaldyrk commented 1 year ago

tq for answer