[SOLVED] Checking the Validity of Session Token

I’m able to get the session token for a particular user. Now, I’m sending this “sessionToken” to the backend where it will check if the session token that is sent is a valid one or not. How can I achieve this?

Referred to other similar threads but didn’t get any solution.

you query the session table from the server and compare with what is on server, you will need to use master key, as an alternative you can also connect directly to mongo db to read data from that session table

I went with the first option - Querying the session table and comparing it.

But I’m facing problem in the backend. When I do a query like this,

async function verifysessiontoken(sessionToken) {
  await Moralis.start({ serverUrl, appId, masterKey });
  const query = new Moralis.Query("_Session")
  const results = await query.find({useMasterKey:true});
  console.log('SESSION RESULT: ', results)
  return results

The query successfully returns the data like this in the backend - (Can’t find session token anywhere)

Whereas in the frontend it shows the data that I expect -

Why is that the same response shows differently in backend console and frontend console. I actually need just like the data shown in the frontend ( the session tokens and other stuff…) in my backend to perform validation.

try to access it directly, even if you don’t see it in that output

Tried that but getting undefined as response. Any other way to access? If there is any documentation related to this, it would be very helpful.

try something like this:

    query = new Moralis.Query("_Session");
    result = query.find({useMasterKey:true})
      function(value) {
        value.forEach((a) => console.log(a.get('sessionToken')));
      function(error) {console.log(error);}
1 Like

Thanks a lot. It worked…!! But using .then didn’t work for me. It threw an error. So I tried with a.get('sessionToken') and it worked as expected.

1 Like