# Storage

The library requires an `IStorage` implementation to persist authentication data (token, roles, profile) across requests.

## IStorage Interface

```typescript
interface IStorage {
  getItem(key: string): Promise<string>;
  setItem(key: string, value: any): Promise<void>;
  removeItem(key: string): Promise<void>;
}
```

**Note:** `getItem` must return an empty string (not `null` or `undefined`) when the key is not found.

## Built-in Implementations

### LocalStorage (default)

Wraps the browser's `window.localStorage`. Used automatically if no storage is provided:

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

const iam = createIamClient({
  apiUrl: 'https://server.com/api',
  storage: new LocalStorage() // this is the default
});
```

### MemoryStorage

In-memory storage, useful for Node.js, testing, and SSR environments:

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

const iam = createIamClient({
  apiUrl: 'https://server.com/api',
  storage: new MemoryStorage()
});
```

Data is lost when the process exits or the instance is garbage collected.

## Custom Storage

Implement the `IStorage` interface for custom persistence (e.g. AsyncStorage for React Native, SecureStore for Expo, IndexedDB):

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

class AsyncStorageAdapter implements IStorage {
  async getItem(key: string): Promise<string> {
    return (await AsyncStorage.getItem(key)) || '';
  }

  async setItem(key: string, value: any): Promise<void> {
    await AsyncStorage.setItem(key, String(value));
  }

  async removeItem(key: string): Promise<void> {
    await AsyncStorage.removeItem(key);
  }
}

const iam = createIamClient({
  apiUrl: 'https://server.com/api',
  storage: new AsyncStorageAdapter()
});
```

## Stored Keys

The `AuthService` uses the following storage keys:

| Key            | Content                                   |
| -------------- | ----------------------------------------- |
| `token`        | JWT auth token                            |
| `roles`        | JSON array of role strings                |
| `username`     | Username                                  |
| `profile`      | JSON object with profile data             |
| `email`        | User email                                |
| `permissions`  | Comma-separated permission strings        |
| `impersonate`  | `'true'` / `'false'` impersonation flag   |
| `admin_*`      | Backup of above keys during impersonation |
| `deviceCode`   | Registered device code                    |
| `deviceSecret` | Registered device secret                  |
