Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

I see this snippet:

app.addHook('onRequest', (req, reply, done) => {
  // overwrite the defaults
  req.requestContext.set('user', { id: 'helloUser' });
  done();
});

// this should now get `helloUser` instead of the default `system`
app.get('/', (req, reply) => {
  const user = req.requestContext.get('user');
  reply.code(200).send( { user });
});

And I wonder: what's the difference from:

app.decorateRequest('user', null);
app.addHook('onRequest', (req, reply, done) => {
  // overwrite the defaults
  req.user = { id: 'helloUser' }
  done();
});

// this should now get `helloUser` instead of the default `system`
app.get('/', (req, reply) => {
  const user = req.user;
  reply.code(200).send( { user });
});

?

I've used the above in a app that runs in pm2 cluster mode and it seems to work. Does this mean it's currently susceptible to a race condition?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.