Skip to content

Instantly share code, notes, and snippets.

@galvez
Created June 23, 2020 04:34
Show Gist options
  • Save galvez/a5370809cd518bb0c1e7e2fa66d7bbcb to your computer and use it in GitHub Desktop.
Save galvez/a5370809cd518bb0c1e7e2fa66d7bbcb to your computer and use it in GitHub Desktop.

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