Skip to content

feat: Add better docs for setting the user #12224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ Serverside SDKs that instrument incoming requests will attempt to pull the IP ad

If the user's `ip_address` is set to `"{{auto}}"`, Sentry will infer the IP address from the connection between your app and Sentry's server.

If the field is omitted, the default value is `null`. However, due to backwards compatibility concerns, certain platforms (in particular JavaScript) have a different default value for `"{{auto}}"`. SDKs and other clients should not rely on this behavior and should set IP addresses or `"{{auto}}"` explicitly.
If the field is omitted, the default value is `"{{auto}}"`. SDKs and other clients should not rely on this behavior and should set IP addresses or `"{{auto}}"` explicitly.

To opt out of storing users' IP addresses in your event data, you can go to your project settings, click on "Security & Privacy", and enable "Prevent Storing of IP Addresses" or use Sentry's [server-side data](/security-legal-pii/scrubbing/) scrubbing to remove `$user.ip_address`. Adding such a rule ultimately overrules any other logic.
To ensure your users' IP addresses are never stored in your event data, you can go to your project settings, click on "Security & Privacy", and enable "Prevent Storing of IP Addresses" or use Sentry's [server-side data scrubbing](/security-legal-pii/scrubbing/) to remove `$user.ip_address`. Adding such a rule ultimately overrules any other logic.

</DefinitionList>

Additionally, you can provide arbitrary key/value pairs beyond the reserved names, and the Sentry SDK will store those with the user.

You'll first need to import the SDK, as usual:
## Setting the User

<PlatformContent includePath="enriching-events/import" />

To identify the user:
You can set the user by calling the `setUser` method on the Sentry SDK:

<PlatformContent includePath="enriching-events/set-user" />

You can also clear the currently set user:

<PlatformContent includePath="enriching-events/unset-user" />

<PlatformCategorySection supported={["server"]}>
<PlatformSection notSupported={['javascript.bun', 'javascript.cloudflare', 'javascript.deno', 'javascript.react-router']}>
`Sentry.setUser()` will set the user for the currently active request - see <PlatformLink to="/enriching-events/request-isolation">Request Isolation</PlatformLink> for more information. For example, if you want to set the user for a single request, you can do this like this:
</PlatformSection>
<PlatformContent includePath="enriching-events/set-user-request" />

Or if you want to set the user for all requests, you could use a middleware like this:

<PlatformContent includePath="enriching-events/set-user-middleware" />
</PlatformCategorySection>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```javascript
// Add a middleware, for example:
app.use((req, res, next) => {
// Get the user from somewhere
const user = req.user;

// Set the user data for all requests
if (user) {
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
});
} else {
Sentry.setUser(null);
}

next();
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```javascript
const Sentry = require("@sentry/node");

// Add a middleware, for example:
app.use((req, res, next) => {
// Get the user from somewhere
const user = req.user;

// Set the user data for all requests
if (user) {
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
});
} else {
Sentry.setUser(null);
}

next();
});
```
16 changes: 16 additions & 0 deletions platform-includes/enriching-events/set-user-request/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```javascript
// Your route handler, for example:
app.get("/my-route", (req, res) => {
// Get the user from somewhere
const user = req.user;

// Set the user data for this request only
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
});

res.send("Hello World");
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```javascript
const Sentry = require("@sentry/node");

// Your route handler, for example:
app.get("/my-route", (req, res) => {
// Get the user from somewhere
const user = req.user;

// Set the user data for this request only
Sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
});

res.send("Hello World");
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```javascript
const Sentry = require("@sentry/node");

Sentry.setUser({ email: "john.doe@example.com" });
```
Loading