Skip to content

Commit 1cf3be9

Browse files
committed
feat: Add better docs for setting the user
1 parent 5f28814 commit 1cf3be9

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
lines changed

docs/platforms/javascript/common/enriching-events/identify-user/index.mdx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,30 @@ Serverside SDKs that instrument incoming requests will attempt to pull the IP ad
2626

2727
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.
2828

29-
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.
29+
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.
3030

31-
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.
31+
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](/security-legal-pii/scrubbing/) scrubbing to remove `$user.ip_address`. Adding such a rule ultimately overrules any other logic.
3232

3333
</DefinitionList>
3434

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

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

39-
<PlatformContent includePath="enriching-events/import" />
40-
41-
To identify the user:
39+
You can set the user by calling the `setUser` method on the Sentry SDK:
4240

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

4543
You can also clear the currently set user:
4644

4745
<PlatformContent includePath="enriching-events/unset-user" />
46+
47+
<PlatformCategorySection supported={["server"]}>
48+
`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:
49+
50+
<PlatformContent includePath="enriching-events/set-user-request" />
51+
52+
Or if you want to set the user for all requests, you could do this with a middleware like this:
53+
54+
<PlatformContent includePath="enriching-events/set-user-middleware" />
55+
</PlatformCategorySection>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```javascript
2+
// Add a middleware, for example:
3+
app.use((req, res, next) => {
4+
// Get the user from somewhere
5+
const user = req.user;
6+
7+
// Set the user data for all requests
8+
if (user) {
9+
Sentry.setUser({
10+
id: user.id,
11+
email: user.email,
12+
username: user.username,
13+
});
14+
} else {
15+
Sentry.setUser(null);
16+
}
17+
18+
next();
19+
});
20+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```javascript
2+
const Sentry = require("@sentry/node");
3+
4+
// Add a middleware, for example:
5+
app.use((req, res, next) => {
6+
// Get the user from somewhere
7+
const user = req.user;
8+
9+
// Set the user data for all requests
10+
if (user) {
11+
Sentry.setUser({
12+
id: user.id,
13+
email: user.email,
14+
username: user.username,
15+
});
16+
} else {
17+
Sentry.setUser(null);
18+
}
19+
20+
next();
21+
});
22+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
```javascript
2+
// Your route handler, for example:
3+
app.get("/my-route", (req, res) => {
4+
// Get the user from somewhere
5+
const user = req.user;
6+
7+
// Set the user data for this request only
8+
Sentry.setUser({
9+
id: user.id,
10+
email: user.email,
11+
username: user.username,
12+
});
13+
14+
res.send("Hello World");
15+
});
16+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
```javascript
2+
const Sentry = require("@sentry/node");
3+
4+
// Your route handler, for example:
5+
app.get("/my-route", (req, res) => {
6+
// Get the user from somewhere
7+
const user = req.user;
8+
9+
// Set the user data for this request only
10+
Sentry.setUser({
11+
id: user.id,
12+
email: user.email,
13+
username: user.username,
14+
});
15+
16+
res.send("Hello World");
17+
});
18+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```javascript
2+
const Sentry = require("@sentry/node");
3+
4+
Sentry.setUser({ email: "[email protected]" });
5+
```

0 commit comments

Comments
 (0)