# React Admin Integration

The `ApplicaAuthProvider` adapter implements the React Admin `AuthProvider` contract, delegating internally to `IamClient`.

## Setup

```typescript
import { createAuthProvider } from '@applica-software-guru/iam-client';

// Backward-compatible: accepts URLs ending with /auth (automatically stripped)
const authProvider = createAuthProvider({
  apiUrl: 'https://server.com/api/auth'
});
```

Usage in React Admin:

```tsx
import { Admin } from 'react-admin';

<Admin authProvider={authProvider}>{/* resources */}</Admin>;
```

## React Admin AuthProvider Methods

The provider implements all standard `AuthProvider` methods:

```typescript
await authProvider.login({ username, password });
await authProvider.logout();
await authProvider.checkAuth();
await authProvider.checkError(error); // React Admin specific
await authProvider.getPermissions();
await authProvider.getIdentity();
```

`checkError` rejects on HTTP 401/403 errors, which triggers React Admin's automatic redirect to the login page.

## Extended Methods

In addition to the standard `AuthProvider` contract, the provider also exposes:

```typescript
// Token management
await authProvider.getToken();
await authProvider.getHeaders();
await authProvider.validateToken(token);

// Roles
await authProvider.getRoles();

// Impersonation
await authProvider.impersonate(userId);
await authProvider.isImpersonating();
await authProvider.stopImpersonate();

// Device and PIN
await authProvider.registerDevice('device-code');
await authProvider.getDevice('device-code');
await authProvider.pinLogin('device-code', '1234');
await authProvider.resetPin('user-id', '5678');

// Registration and account management
await authProvider.register({ name, email, password });
await authProvider.activate('activation-code');
await authProvider.changePassword('currentPw', 'newPw');
await authProvider.recover('user@test.com');
await authProvider.updateProfile({ name: 'New Name' });
await authProvider.thirdPartyLogin({ provider: 'google', payload });
```

## Accessing the Full IamClient

Use `getClient()` to access all services (users, tenants, projects, roles, devices):

```typescript
const iam = authProvider.getClient();

// Now you can use all services
const tenants = await iam.tenants.search({});
const { user } = await iam.users.getById('user-id');
await iam.roles.register({ code: 'EDITOR', name: 'Editor' });
```

This is useful when your React Admin application needs to manage entities beyond authentication.

## URL Normalization

For backward compatibility, if the `apiUrl` ends with `/auth`, it is automatically stripped. This means both of these work:

```typescript
// Both produce the same result:
createAuthProvider({ apiUrl: 'https://server.com/api/auth' });
createAuthProvider({ apiUrl: 'https://server.com/api' });
```
