Nyxynyx
Nyxynyx

Reputation: 63599

Find the Current Session Id in Meteor.js

How do you find the current Session Id on the client?

I am able to get what seems like the last session id, not the current session id.

console.log(Meteor.default_connection._lastSessionId)

Upvotes: 6

Views: 4134

Answers (2)

Faiz Mohamed Haneef
Faiz Mohamed Haneef

Reputation: 3596

The Meteor login token by default is stored in local storage (not in cookie).

On the client you could access

token = Meteor._localStorage.getItem('Meteor.loginToken')

On the server, once token is received, use the Accounts api to hash using

Accounts._hashLoginToken(res.req.body.token)

Then, you could validate hashed value against users collection for services.resume.loginTokens.hashedToken field

This hack can be used to build meteor - express integration

Meteor Login Token

Upvotes: 2

Tarang
Tarang

Reputation: 75945

The wording for this is a bit confusing, but _lastSessionId is the current session id.

It is only called this because if the client is disconnected and seeks to reconnect it wants to re-establish the session with the last session id.

The client would reconnect with a message like this :

{"msg": "connect ", "session": "ERoZSR3R3f8zBQ6Ry", "version": "pre1","support":["pre1"]}

The session uses the lastSessionId value. This is then used to re-establish the previous connection.

This is the only case where a new session id would be assigned on a reconnection. That or the session is expired off the server.

If the server restarts it's cache is refreshed and it wouldn't recognize the session anymore, and a new session id would be assigned.

Upvotes: 8

Related Questions