0

So I have a user stored in session when they log in on my Node.js application. I have admins and default users, if I output #{session.user.role} in the Jade template, I get default or admin appear. And when testing on my page I can hide items on the page using -if(session.user) to hide an item if no one is logged in, but how do I hide items for users which aren't admins?

I have tried putting in -if(session.user.role === 'default') but I get an error saying Cannot read property 'role' of undefined. So that won't work, any ideas on how to do this anyone?

Here is the User Schema using Mongoose:

var User = new Schema({ 'key' : { unique : true, type : Number, default: getId }, 'username' : { type : String, validate : [validatePresenceOf, 'a Username is required'], set : toLower, index : { unique : true } }, 'password' : String, 'role' : String, }); 
1
  • You're saying session.user.role outputs default or admin, so it must exist. You sure the undefined-error doesn't just occur for users that are not logged in? If so you could write if(session.user && session.user.role === 'default'). Commented Apr 14, 2013 at 20:35

1 Answer 1

3
- if (typeof session.user != 'undefined' && session.user.role === 'default') 
Sign up to request clarification or add additional context in comments.

2 Comments

Out of interest, would you know how to do this in an express route for the Node.js app? I'm routing to a page, but want to shut off access to the page if the user is not an admin. I'm just using a log to console at the moment to print to the console if the user is an admin or not but I get a session is not defined error.
Thanks for the insight, how would you check for someone who is a default user OR not registered? For example if a user is set as default or as admin the page will load, but if no user is logged in, the error is role is not defined as no one is logged in

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.