npm package: @newgameplusinc/odyssey-sso
Description: Browser SDK for Odyssey SSO Service, handling authentication, user profile, and resource management.
Author: Ashik
Latest version: 2.2.3

# Odyssey SSO SDK

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

---

## Installation

```bash
npm install @newgameplusinc/odyssey-sso
```

---

## Usage

```ts
import SSO from "@newgameplusinc/odyssey-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();

// 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) |

---

## Methods

### `login(redirectUrl: string): Promise<void>`

Redirects the user to the SSO login page.

* **Parameters:**

  * `redirectUrl` — URL to redirect after login

---

### `exchange(): Promise<{ tokens; userData } | undefined>`

Exchanges the SSO token from the URL for access/refresh tokens and stores the profile locally.

* **Returns:** User profile + tokens or `undefined` if exchange fails

---

### `fetchProfile(): Promise<Profile | undefined>`

Fetches the user profile from the SSO server.

* **Returns:** Profile object

---

### `refresh(): Promise<{ accessToken: string; refreshToken: string } | undefined>`

Refreshes the access token using the stored refresh token.

* **Returns:** Updated tokens

---

### `logout(): Promise<boolean | undefined>`

Logs out the user from the SSO server and clears local storage.

* **Returns:** `true` on success

---

### `onEvents(cb: Cb)`

Subscribe to SDK events. The `cb` callback will be called with an event object containing a `type` and `payload`.

**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 events:**

* **cloth.add** — cloth added
* **cloth.fetch-all** — all cloths fetched
* **cloth.fetch** — single cloth fetched
* **cloth.update** — cloth updated
* **cloth.delete** — cloth deleted

**Material events:**

* **material.add** — material added
* **material.fetch-all** — all materials fetched
* **material.fetch** — single material fetched
* **material.update** — material updated
* **material.delete** — material deleted

**Skull events:**

* **skull.add** — skull added
* **skull.update** — skull updated
* **skull.fetch-all** — all skulls fetched
* **skull.fetch** — single skull fetched
* **skull.delete** — skull deleted

---

## Profile Management

### `profileUpdate(payload: UpdateProfilePayload)`

Update user profile.

### `avatarUpdate(file: File)`

Update user avatar by uploading a `File`.

---

## Cloth Management

* `clothAdd(payload: AddClothFilePayload)`
* `clothGetAll(options?: GetClothsOptions)`
* `clothGetById(clothId: string)`
* `clothUpdate(clothId: string, payload: UpdateClothPayload)`
* `clothDelete(clothId: string)`

All methods handle file uploads and token refresh automatically.

---

## Material Management

* `materialAdd(payload: AddMaterialFilePayload)`
* `materialGetAll(options?: GetMaterialsOptions)`
* `materialGetById(materialId: string)`
* `materialUpdate(materialId: string, payload: UpdateMaterialPayload)`
* `materialDelete(materialId: string)`

---

## Skull Management

* `skullAdd(payload: AddSkullFilePayload)`
* `skullGetAll()`
* `skullGetById(skullId: string)`
* `skullUpdate(skullId: string, payload: UpdateSkullPayload)`
* `skullDelete(skullId: string)`

---

## 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.