Help on how to get active login session using ddpclient

Hello, it’s me again. I just want to get some help or ideas on how to get any active login session from a separate meteor app to a separate NextJs app that is using a DDP client (asteroid or simpleddp). Basically, how will nextjs app through DDP knows that there is an active session on the meteor app? It might be a dumb question for some but I am dumb as hell and I haven’t tried asteroid or simpleddp, maybe the answer is right in front of my eyes or just fetching a login session from a database is the answer. Hmmm idk

Hello there! Not sure if you solved this one, but since you are using a Next(ssr) app probably, you will need to do some middleware thing always to make sure your user is logged in or not

// you can place this helper in a separate file so that it can be reused
async function initDDP(req, res) {
  const s = new DDPClient(opts); // same way you would in the server.  
  await s.connect();
  const userId = req?.headers?.cookie // you just need to send the userID in the request(he is logged)
  // get the full user if you want
  let user = await server.collection('users').filter(user=>user.id==userId).fetch()[0];
  return s;
}

export async function getServerSideProps({ req, res }) {
  const s = await initDDP(req, res)

Have not tested with Next but I think it would work.

If you want just to set the userId in the connection, you can use the .setUserId method

you can have something like this in your Meteor Server

Meteor.methods(
  {
    setCurrentId(_id) { 
      check(id, String);
      if (this.userId) throw new Error("has already an Id");
      if (!Meteor.users.findOne({ _id: _id })) throw new Meteor.Error(404, "User not found.");
    
      this.setUserId(_id);
    }
  }
)