Search code examples
node.jsexpressnpmstormpath

Node.js-Express-Strompath No CustomData after immediately after registration


I am having a strange error (at least in my opinion) after registering a user to stormpath via node/express using the Express-Stormpath module.

Just after registering I seem to have no access to customData in various routes for just the first user session. On registration I am creating and array for invoices

app.use(stormpath.init(app, {
    ...,
    expandCustomData: true,
    postRegistrationHandler: function(account, req, res, next) {

        account.customData.invoices = [];
        account.save();

        next();
    }
}));

but then when I go to access them in my index route I get this error

router.get('/', stormpath.loginRequired, function(req, res){

    console.log(req.user.customData.invoices ); // undefined

     res.render('index', {
        title: 'Index'
     });
});

how ever if I kill my local and restart it I get

 console.log(req.user.customData.invoices ); // []

which is what i would like.

Can anyone shed some light into what i am doing incorrectly here?

Thx in advance.


Solution

  • What's happening here is this: when you're inside of the postRegistrationHandler code -- customData won't be available automatically by default. The postRegistrationHandler is called immediately after registration before any of the helper functions have fun.

    What you need to do to make your example work is first 'fetch' the customData from the Stormpath service.

    Here's a working example:

    app.use(stormpath.init(app, {
      ...,
      expandCustomData: true,
      postRegistrationHandler: function(account, req, res, next) {
        account.getCustomData(function(err, data) {
          if (err) return next(err);
          data.invoices = [];
          data.save();
          next();
        });
      }
    }));
    

    The above issue is really unclear in the docs -- this is 100% my fault (I'm the author of the library) -- I'll get this fixed today =)