Search code examples
node.jsexpresspugstormpath

Getting group name from account in Stormpath-Express


I am making a Stormpath-Express application for a company configured with many groups. Each group can only see certain elements on the page (ie. admins can delete and approve posts, users can only create and edit them). Is it possible to use Jade to create a conditional that will display the component by getting the user's group membership?

Here's a rough example to show what I mean:

if user.admin === true
    a.btn.submit Approve Post
else 
    p Please wait for management to approve

I have noticed I can use user.fullName, user.email, etc. but not user.directory.

I have also tried this through the server side using getDirectory method

client.getDirectory(href, function(err, dir) {
    console.log(dir);
});

However, this is from the Node docs, and breaks when applied to my Express project. Any help with this is greatly appreciated since I'm in a deadline!


Solution

  • Much thanks to rdegges answer, I had to modify with the version 1.0.5 express-stormpath because the groups property is used to store the uri to retrieve the groups...

    app.get('/page', stormpath.loginRequired, function(req, res) {
      req.user.groupsNames = [];
    
      req.user.getGroups(function(err, groups) {
        if (err) return next(err);
    
        groups.each(function(group, cb) {
          req.user.groupsNames.push(group.name);
          cb();
        }, function(err) {
          if (err) return next(err);
          return res.render('myview');
        });
      });
    });
    

    And the jade template looks like...

    - if (user.groupNames.indexOf('admin') > -1)
      a.btn.submit Approve Post
    - else
      p Please wait for management to approve