# Odyssey SSO SDK

The **Odyssey SSO SDK** provides an easy way to integrate Single Sign-On (SSO) functionality, user profile management, and resource management in your browser-based applications.

---

## Installation

```bash
npm install @alliumcloud/avatar-sso
```

---

## Usage

```ts
import SSO from "@alliumcloud/avatar-sso";

const sso = new SSO({
  sdkKey: process.env.NEXT_PUBLIC_SDK_KEY,
  debug: true,
});

// Login
sso.login("https://your-app.com/redirect");

// Exchange token from URL and get profile (put it in the root component so that it has access to the url all the time)
const user = await sso.exchange();

// Fetch user profile
const profile = await sso.fetchProfile();

// Refresh token
const tokens = await sso.refresh();

// Logout
await sso.logout("https://your-app.com/redirect");

// Listen to SDK events
sso.onEvents((event) => {
  console.log("SSO Event:", event);
});
```

---

## SDK Configuration

| Field    | Type    | Description                  |
| -------- | ------- | ---------------------------- |
| `sdkKey` | string  | Your SSO SDK key             |
| `debug`  | boolean | Enable debug logs (optional) |

---

## Public Methods

### Auth

* `localLogin(email: string, password: string)`
* `localRegister(email: string, password: string, username: string)`
* `localLogout()`
* `login(redirectUrl: string)`
* `exchange()`
* `fetchProfile()`
* `refresh()`
* `logout(redirectUrl: string)`

### Profile

* `profileUpdate(payload: UpdateProfilePayload)`
* `avatarUpdate(file: File)`

### Cloth

* `clothGetAll(options?: GetClothsOptions)`
* `clothGetById(clothId: string)`

### Material

* `materialGetAll(options?: GetMaterialsOptions)`
* `materialGetById(materialId: string)`

### Skull

* `skullGetAll()`
* `skullGetById(skullId: string)`

### Skull Material

* `skullMaterialGetAll()`
* `skullMaterialGetById(id: string)`

### Body

* `bodyGetAll()`
* `bodyGetById(id: string)`

### Events

`onEvents(cb: Cb)` subscribes to SDK events.

**Event types:**

* **login** — user logged in
* **logout** — user logged out
* **refresh** — token refreshed
* **profile.fetch** — profile fetched
* **profile.update** — profile updated
* **profile-photo.update** — avatar updated
* **cloth.add** — cloth added
* **cloth.fetch-all** — all cloths fetched
* **cloth.fetch** — single cloth fetched
* **cloth.update** — cloth updated
* **cloth.delete** — cloth deleted
* **material.add** — material added
* **material.fetch-all** — all materials fetched
* **material.fetch** — single material fetched
* **material.update** — material updated
* **material.delete** — material deleted
* **skull.add** — skull added
* **skull.update** — skull updated
* **skull.fetch-all** — all skulls fetched
* **skull.fetch** — single skull fetched
* **skull.delete** — skull deleted
* **skull-material.add** — skull material added
* **skull-material.fetch-all** — all skull materials fetched
* **skull-material.fetch** — single skull material fetched
* **skull-material.update** — skull material updated
* **skull-material.delete** — skull material deleted
* **body.add** — body added
* **body.fetch-all** — all bodies fetched
* **body.fetch** — single body fetched
* **body.update** — body updated
* **body.delete** — body deleted

---

## Notes

* This SDK is **browser-only**. All methods check for the browser environment.
* Uses a local database (`IndexedDB`) to store tokens and user profile for session persistence.
* Automatically handles token refresh for API requests.
