How can I fetch current Session from currentUser?

How can I fetch current Session token from client?

  1. User login through Moralis
  2. Client browser has currentUser.
    Ex. const currentUser = Moralis.User.current();
  3. Somehow I need to fetch the current session from client.

If user accesses my website with two different devices. For example, iPhone, MacBook. The user has 2 valid sessions.

I wanna pick the current session that user is using on the current devise.

Is it possible?

1 Like

do you mean in the extreme case when they both at logged in on as you say “macbook” and “iphobe” at yhe same time??

i dont think anything changes, the only thing youd care abut here is the web3 instance. say you connect to walletconnect on your phone and then you login with metamask (different account) on your computer, these would be two active sessions of the same uswr but it doesnt make a difference. So it you shouldnte to make any changes at all that would cause things to work not as expected in my mind anyways. i think i dont fully get yout question theres is obviously something your trying to test. or maybe im just reading your question wrong, apppologies if I am

1 Like

Thanks for the comment.

So my question is how can I get the current active session.
The user has 2 active sessions. One is generated from iPhone. The other is from MacBook.

I need to pick the current session. How can I do that?

If I search Session based on currentUser id, maybe I’ll see 2 sessions. I still cannot figure out which one is which.

Or how can I get an installationId of the current session from client?

somewhere should be a session id, in cookies, in user info

Yeah I can see the installationId in local storage.

But the key is like this “Parse/ypmd4sVkBoqHJlMTIBbZMaN6Jke7Tst8TbPQyYAA/installationId”

How can I know this key?

Maybe look in session table to see how the session id looks like. You may not be able to see it in the dashboard but you could make a query to get it.

How can I query it from client?

The client knows about currentUser and local storage info.
If I query session by currentUser.id it will pick multiple sessions. Cause one user can have many sessions.

Once I get installationId from local storage, it can point out specific session based on userId and installationId, right?

How can I get installationId or current sessionToken from client?

My question is the way to fetch the current token from client side. Not server side.
My client is React.js which works on customer’s web browser.

What I’m trying to achieve is to get the current session token from web browser (React.js)

It should be possible, you should be able to read the browser cookies at least.

If I do like this.

localStorage.getItem(‘Parse/ypmd4sVkBoqHJlMTIBbZMaN6Jke7Tst8TbPQyYAA/installationId’)

I can get the current session’s installationId
‘a2f8f727-74bf-439e-b0c9-1df10d2ef066’

I know the key (Parse/ypmd4sVkBoqHJlMTIBbZMaN6Jke7Tst8TbPQyYAA/installationId) Cause I checked it from browser’s dev tool.

But React.js code cannot do such things. It needs the key.

I want to do it programatically.

My question is very very simple. I think almost everyone knows it, I guess.
How can I get current session from client?

You can use simple JavaScript code in react too

Yeah, JavaScript is ok.

How should I write JavaScript code to fetch the current session token?

I found the answer.

currentUser.attributes.sessionToken

1 Like