Identifying users
When a user is logged into your application and an error or trace occurs, helpful information about the user can be sent to Flare.
To do this, extend the UserAttributesProvider base class and override the methods you care about. The user is available as $this->user inside each method:
use Spatie\FlareClient\AttributesProviders\UserAttributesProvider;
class CustomUserAttributesProvider extends UserAttributesProvider
{
public function id(): string|int|null
{
return $this->user->id;
}
public function fullName(): ?string
{
return "{$this->user->first_name} {$this->user->last_name}";
}
public function email(): ?string
{
return $this->user->email;
}
public function attributes(): array
{
return [
'team_id' => $this->user->team_id,
];
}
}
Implementing all these methods is not required; you can mix and match the ones you need.
The attributes method can return a key-value array with extra information about the user other than its name, email, or ID.
Passing the user to recorders
Attribute providers are passed directly to the recorder methods that need them. When you end a request, pass an instance of your provider:
$flare->request()->recordEnd(
userAttributesProvider: new CustomUserAttributesProvider($currentUser),
);
If your framework integration ends requests through one of the helpers, the helpers also accept a userAttributesProvider argument:
$flare->request()->recordEndFromSymfonyResponse(
response: $response,
userAttributesProvider: new CustomUserAttributesProvider($currentUser),
);
Pulling the user out of the request
If your RequestAttributesProvider already has access to the request object (and the request can produce the user), you can override the user resolution there. Implement RequestAttributesProvider and have it return the user via your custom provider; the request recorder picks it up automatically when the same provider exposes user information through its attributes.
The same provider pattern applies to all recorders that record framework objects. See Attribute providers for a complete tour of the providers Flare ships with and how to write your own.