sentry-module icon indicating copy to clipboard operation
sentry-module copied to clipboard

Passing user information to Sentry

Open darrenchiu opened this issue 3 years ago • 4 comments

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.

darrenchiu avatar Jan 31 '22 09:01 darrenchiu

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.

rchl avatar Jan 31 '22 10:01 rchl

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!

darrenchiu avatar Jan 31 '22 14:01 darrenchiu

@rchl works perfectly, closing it now. Thanks so much for your help.

darrenchiu avatar Feb 01 '22 08:02 darrenchiu

I'd want to at least expand documentation. Any help welcome. :)

rchl avatar Feb 01 '22 08:02 rchl