Get and Set session value

Hello Guys,
I’m creating a multipage application in meteor using angular js.
I have used Iron-router to router to different pages.
Before going to a particular page i want to check if session exists or not how can i achieve that ?

Hi,

Since Session is a meteor package (and not Blaze related), you can certainly use session.

The functions are the following :

Session.set(nameOfSessionVariable, value);
Session.get(nameOfSessionVariable);
Session.equals(nameOfSessionVariable, comparaisonValue)

Altough Session are easy to use, I suggest to use ReactiveDict or ReactiveVar for state changes.

If you mean user session, you can just check Meteor.user() or Meteor.userId(). This requires the accounts-base package in your app.

You could do this in the onBeforeAction hook for the route in question. Look at this page for more information. http://iron-meteor.github.io/iron-router/#hooks For instance you could check the session exists and redirect to a different page if it doesn’t.

Thanks @karthons
how can I access the same session variable in different templates?

What do you mean by same variable ? Session variable are set globally in the window scope and thus you can call them in every template.

If you need one in a template, use

 Template.templateName.onCreated(function(){
 Session.set("mySessionVariable", "someValue");
 })

You can try it in the browser console aswell.

@karthons

If i set session in one template and try to get the session value in other template.
i’m not able to access that value
How can I get the session value in other template ?

Example:
Template.NameQ.onCreated(function(){
Session.set(“mySessionVariable”, “someValue”);
})

Template.NameQQ.onCreated(function(){
Session.get(“mySessionVariable”);
})