Getting group name from account in Stormpath-Express

I am making a Stormpath-Express application for a company set up with many teams. Each group can only see certain items on the page (for example, administrators can delete and approve posts, users can create and edit them). Can Jade be used to create a conditional expression that will render the component after gaining membership in a custom group?

Here's an example to show what I mean:

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

      

I noticed that I can use user.fullName

, user.email

etc., but not user.directory

.

I have also tried this server side using getDirectory method

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

      

However this is from Node docs and breaks when applied to my express project. Any help with this is greatly appreciated as I am on a deadline!

+3


source to share


2 answers


Thanks a lot for rdegges' answer, I had to modify version 1.0.5 of express-stormpath because the groups property is used to store the uri to fetch 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 pattern looks like ...

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

      

+2


source


Heyo - I'm the author of the express assault library, thought I'd jump here.

I'm going to add some new ways to do this shortly, but for now you can preload user groups into an array and then check membership. Here's an example:

app.get('/page', stormpath.loginRequired, function(req, res) {
  req.user.groups = [];

  req.user.getGroups(function(err, groups) {
    if (err) return next(err);

    groups.each(function(group, cb) {
      req.user.groups.push(group);
      cb();
    }, function(err) {
      if (err) return next(err);
      return res.render('myview');
    });
  });
});

      



Now that you have defined req.user.groups

, you can use it in your Jade template, for example:

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

      

+1


source







All Articles