14

I am quite new to sailsjs and nodejs. I am trying to create an authentication page, wherein once a user is authenticated, I want to set

req.session.user = user.id
req.session.authenticated = true

I then need to access these values within my main layout.ejs file. I have done so by using

res.locals.user = _.clone( req.session.user )

However, I noticed that this clone method has to be called in every function of every controller so as to allow me to be able to access the user from within the views. Is there a better way of accessing session variables in sailsjs globbaly, without having to clone the req.session in every controller. An example of what I am doing can be found here:

http://pastebin.com/HyE2H4Kq

As you can see, I have called the clone method at the beginning of various functions within the controller.

kushaldsouza
  • 710
  • 5
  • 12
  • 36
  • If the user id has been assigned to `req.session.user`, you can reference a variable named `user` in the view file and call `res.view({user: req.session.user})` in the controller action. – bnuhero Feb 23 '14 at 05:57
  • @bnuhero: Thanks for the reply. As stated in my post, this is what I do not want to do. Since the user session details are needed to be accessed from within layout.ejs file, I do not want to pass the user details from within every controller action to the view. Is there any way to access the session details globally, or to set values which can be accessed from within any view, without explicitly being set on every controller action? – kushaldsouza Feb 23 '14 at 11:45
  • You can define a custom middleware to achieve this. For details, visit this [link](http://stackoverflow.com/questions/20267550/what-is-the-proper-way-to-integrate-dynamic-content-into-the-layout-ejs-file-in) – bnuhero Feb 23 '14 at 13:03

3 Answers3

24

The req object is available in your views by default, unless you overwrite res.locals completely. So you can access a session variable in your view with <%=req.session.user%> (if you're using EJS). You don't have to copy it explicitly into your locals.

sgress454
  • 24,870
  • 4
  • 74
  • 92
0

for anyone using passport.js, session of the user is saved inside req.session.passport.user and you also can access it directly from the view

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
-2

I have finally decided to go with a solution provided here as I found this to be the neatest solution.

I have created a config file called userdata.config in which I have the following code:

module.exports.userdata = {
    guest: true,
    authenticated: false
};

I am then accessing this in the controller as follows:

sails.config.userdata.authenticated = true;
sails.config.userdata.guest = false;
sails.config.userdata.user = sessionUser;

This is then accessed in the view as follows:

sails.config.userdata.user.firstName
Community
  • 1
  • 1
kushaldsouza
  • 710
  • 5
  • 12
  • 36
  • you know that sails.config.userdata will be shared among all requests if it was not cleared? – Gadelkareem Dec 02 '14 at 14:13
  • 1
    Yes. Sails configuration is global across *all requests*, not a single request or session. This is not a good solution unless you only plan on supporting one logged-in user at a time. – sgress454 Jan 06 '15 at 23:21
  • @sgress454 What is your suggestion instead of doing this but with out using `req.session`? Because I'm using JWT authentication, so I don't have session storage. In every request I receive the token and in the payload it has the user ID so how can I make globally accessed that info for every request? – alexventuraio Feb 23 '16 at 16:25