# Identifying users


When an error hits one of your users, you want to know who. Set the user on the scope, and Flare shows them on every error report that follows, and counts how many different users ran into each error.

```rust
use flareapp::User;

flareapp::configure_scope(|scope| {
    scope.set_user(
        User::new("42")
            .email("ada@example.com")
            .name("Ada Lovelace"),
    );
});
```

`User::new` takes the id of the user, and that's the only part Flare needs. Everything else is optional:

| Method | Attribute sent | Description |
|---|---|---|
| `User::new(id)` | `user.id` | The unique id of the user. Flare counts affected users by this id. |
| `.email(email)` | `user.email` | Shown as the label of the user. |
| `.name(name)` | `user.full_name` | Shown as the label of the user when there's no email. |
| `.attribute(key, value)` | `user.attributes` | Anything else you'd like to see next to the user. |

## Sending extra attributes

Anything else that helps you understand who you're dealing with can go in as an attribute. The value can be anything that converts into a `serde_json::Value`:

```rust
use flareapp::User;

flareapp::configure_scope(|scope| {
    scope.set_user(
        User::new("42")
            .email("ada@example.com")
            .attribute("plan", "pro")
            .attribute("team_id", 7),
    );
});
```

## Setting the user per request

In a web app, the user changes with every request. With the axum or Actix Web middleware in place, each request has its own scope, so set the user in your handler or in your authentication middleware, and it only ends up on the reports of that request:

```rust
use axum::extract::Path;
use flareapp::User;

async fn show_order(Path(id): Path<u64>, user: AuthenticatedUser) -> String {
    flareapp::configure_scope(|scope| {
        scope.set_user(User::new(user.id.to_string()).email(&user.email));
    });

    // ...
}
```

The next request starts without that user, so you don't have to clear anything at the end. Setting the user in a middleware? Add the Flare middleware after it, as described in [middleware order](/docs/rust/data-collection/scopes#middleware-order). Otherwise the user ends up on the errors of other requests too. For jobs and background tasks, give the work its own scope as described in [scopes](/docs/rust/data-collection/scopes).

## Clearing the user

Outside of a request, the user stays on the scope until you remove it. Do that when the user logs out:

```rust
flareapp::configure_scope(|scope| {
    scope.remove_user();
});
```

## Where the user ends up

The user is attached to error reports: panics, reported errors, messages and the `error!` lines you [report as errors](/docs/rust/logs/introduction#reporting-error-lines-as-errors). It's added when the report is made, so a report from before `set_user` doesn't get it.

## Read more

- [Scopes](/docs/rust/data-collection/scopes): keep the user of one request or job away from the others.
- [Adding custom context](/docs/rust/data-collection/adding-custom-context): add the tenant, the order or anything else you need next to the user.
