Passing user information to Sentry
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
To setUser context just like wt's described here: https://docs.sentry.io/platforms/javascript/enriching-events/identify-user/
What is the current best practice of setting user and pass it to sentry? To be particular, I am using nuxt-auth as well.
Personally for the server side I'm adding a server middleware so that it runs before all other middlewares. A middleware function like:
function(req, res, next) {
process.sentry.configureScope(scope => {
scope.setUser({
ip_address: req.ipAddress,
id: req.user.id,
username: req.user.username,
});
});
next();
};
This sets user per-request. Of course you need to get the data like ip address and user id from request object or similar so you need other middleware before this one that sets it.
As for the client-side, I use a Nuxt plugin:
/** @type {import('@nuxt/types').Plugin} */
export default ({ $sentry, store }) => {
if ($authPlugin.isLoggedIn()) {
$sentry.setUser({
ip_address: store.state.ipAddress
id: $authPlugin.id,
username: $authPlugin.username,
});
}
};
The client-side assumes that on logging out, the page will be reloaded so I don't have to care about clearing the user manually.
I'm not sure if this could be generalized to be included in the module but I guess at least the documentation could document similar approach.
Thanks, will give that a try! I do think that if we can include this in the documentation, then it would be useful for a lot of people!
@rchl works perfectly, closing it now. Thanks so much for your help.
I'd want to at least expand documentation. Any help welcome. :)