# Log in a field agent

Operation ID: `fieldagent.login`

Authenticate with Cognito USER_PASSWORD_AUTH and return access/ID tokens. The token's tenant_id and roles claims are cross-checked against the requested tenant_id and the field_agent role; a mismatch fails the login with 403 even though Cognito itself accepted the credentials. fhir_person_id/person_type are resolved best-effort (Redis cache, then a FHIR Person search) and may be null even on a successful login.

## Public method

`login`

Signature: `fieldAgent.login(request)`

Return type: `Promise<FieldAgentLoginResponse>`

## Authentication

Classification: **PUBLIC**

## Prerequisites

None documented.

## HTTP

`POST /auth/login`

## Path parameters

None.

## Query parameters

None.

## Body parameters

| Name | Type | Required | Format | Allowed values | Default | Nullable | Description |
|---|---|---:|---|---|---|---:|---|
| `body` | [`fieldagent.LoginRequest`](../models/fieldagent.LoginRequest.md) | No |  |  |  | Yes |  |

Request model: [`fieldagent.LoginRequest`](../models/fieldagent.LoginRequest.md)

## Request example

```json
{
  "email": "test.user@example.com",
  "password": "PasswordTest1234$",
  "tenant_id": "tenant-example-001"
}
```

## Success responses

| Status | Shape | Content type | Description |
|---|---|---|---|
| `200` | [`fieldagent.LoginResponse`](../models/fieldagent.LoginResponse.md) | application/json | Field Agent login tokens |

## Success response examples

### 200

```json
{
  "access_token": "example-access-token",
  "cognito_sub": "00000000-0000-4000-8000-000000000000",
  "expires_in": 3600,
  "fhir_person_id": "agent-example-001",
  "id_token": "example-id-token",
  "person_type": "field_agent",
  "refresh_token": "example-refresh-token",
  "token_type": "Bearer"
}
```

## Common errors

| Status | Shape | Content type | Description |
|---|---|---|---|
| `400` | [`fieldagent.ErrorResponse`](../models/fieldagent.ErrorResponse.md) | application/json | Required fields are missing |
| `401` | [`fieldagent.ErrorResponse`](../models/fieldagent.ErrorResponse.md) | application/json | Cognito rejected the credentials |
| `403` | [`fieldagent.ErrorResponse`](../models/fieldagent.ErrorResponse.md) | application/json | The token's tenant or role does not match the request |

## Error examples

### 400 — Required login fields are missing

```json
{
  "error": "email, password and tenant_id are required"
}
```

### 401 — Cognito authentication failed (sanitized example)

```json
{
  "error": "Incorrect username or password."
}
```

### 403 — The account does not carry the field_agent role

```json
{
  "error": "Access denied: field_agent role required"
}
```

### 403 — The token's tenant_id claim does not match the requested tenant_id

```json
{
  "error": "Tenant mismatch"
}
```

## NodeJS / TypeScript implementation

```ts
import { HCSDK } from "@healthcloudai/hc-sdk";
import type { FieldAgentLoginRequest } from "@healthcloudai/hc-sdk";
```

```ts
const request = {
  "email": "test.user@example.com",
  "password": "PasswordTest1234$",
  "tenant_id": "tenant-example-001"
};

const result = await fieldAgent.login(request);
```

## cURL

```bash
curl -X POST \
  -H 'Content-Type: application/json' \
  -d '{"email":"test.user@example.com","password":"PasswordTest1234$","tenant_id":"tenant-example-001"}' \
  'https://dev-api-fieldagent.health.cloud/auth/login'
```

## Notes

None.

## Prepared Test Console scenario

No canonical scenario is currently associated.

## Real response

No approved real integration response is currently published. Unapproved candidates are never rendered as examples.
