> For the complete documentation index, see [llms.txt](https://kbase.whitelabel-loyalty.com/developer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kbase.whitelabel-loyalty.com/developer/resources/typescript-sdk/authentication.md).

# Authentication

{% hint style="info" %}
This page describes the authentication methods used by White Label Loyalty (WLL) services:

* "Admin Authentication" — a WLL-issued administrator token.
* "Tenant Authentication" — a token typically issued by your system for end users.

In this guide, **Admin Authentication** refers to a WLL-issued admin token and **Tenant Authentication** refers to a token issued for an end user to use.
{% endhint %}

{% tabs %}
{% tab title="Admin Authentication" %}
Admin authentication is provided via the `AdminAuthProvider`.

```ts
import { AdminAuthProvider } from '@wll-sdk/api';
```

Example: initializing the WLL Rewards SDK with an `AdminAuthProvider`

{% code title="admin.ts" %}

```ts
import { WLLRewardsSdk, AdminAuthProvider } from '@wll-sdk/api';

new WLLRewardsSdk({
  apiKey: '<your-api-key>',
  authProvider: new AdminAuthProvider({
    clientId: '<your-client-id>',
    clientSecret: '<your-client-secret>',
  }),
  baseUrl: 'https://api.rewards.wlloyalty.net/v1',
});
```

{% endcode %}

The `AdminAuthProvider` will automatically extract which region you are using from the `baseUrl` and attempt to authenticate. Current regions supported by the SDK:

* EU
* US

AdminAuthConfig Reference

| Property       | type      | Required? |
| -------------- | --------- | --------- |
| `clientId`     | `string`  | Y         |
| `clientSecret` | `string`  | Y         |
| `grantType`    | `string?` | N         |
| `audience`     | `string?` | N         |
| `scope`        | `string?` | N         |
| {% endtab %}   |           |           |

{% tab title="Tenant (Static) Authentication" %}
Tenant authentication is provided via the `StaticAuthProvider`.

```ts
import { StaticAuthProvider } from '@wll-sdk/api';
```

The `StaticAuthProvider` is a simple implementation of `AuthProvider` that takes a token in its constructor and always returns that token. It is useful for tenant authentication after completing your authentication flow, or for admin authentication if you store an admin token.

Example: initializing the WLL Rewards SDK with a `StaticAuthProvider`

{% code title="tenant.ts" %}

```ts
import { WLLRewardsSdk, StaticAuthProvider } from '@wll-sdk/api';

async function tenantTokenFlow(): Promise<string> {
  // Your implementation.
}

const token = await tenantTokenFlow();

new WLLRewardsSdk({
  apiKey: '<your-api-key>',
  authProvider: new StaticAuthProvider({
    token,
  }),
  baseUrl: 'https://api.rewards.wlloyalty.net/v1',
});
```

{% endcode %}

StaticAuthConfig Reference

| Property     | type     | Required? |
| ------------ | -------- | --------- |
| `token`      | `string` | Y         |
| {% endtab %} |          |           |

{% tab title="Custom AuthProvider (Implement your own)" %}
If needed, implement your own `AuthProvider`. The abstract class `AuthProvider` is exported by the SDK. Implementing it lets you pass a custom provider to the SDK (for example, fetching a token from a remote cache).

Abstract method signature:

```ts
abstract class AuthProvider {
  public abstract getToken(region: Region): Promise<string>;
}
```

Example: minimal custom auth provider

{% code title="remote-auth-provider.ts" %}

```ts
import { WLLRewardsSdk, AuthProvider } from '@wll-sdk/api';
import { fetchRemoteToken } from '../path/to/fetchRemoteToken';

class RemoteAuthProvider implements AuthProvider {
  public async getToken(region: any): Promise<string> {
    const token = await fetchRemoteToken();
    return token;
  }
}

new WLLRewardsSdk({
  apiKey: '<your-api-key>',
  authProvider: new RemoteAuthProvider(),
  baseUrl: 'https://api.rewards.wlloyalty.net/v1',
});
```

{% endcode %}
{% endtab %}
{% endtabs %}
