> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microapp.io/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Microapp authentication for any web app.

The TypeScript Auth SDK enables you to authenticate users without requiring a particular frontend framework.

## Getting Started

### 1. Install the SDK

Install the SDK with your preferred package manager.

<CodeGroup>
  ```bash npm theme={null}
  npm install @microapp-io/auth
  ```

  ```bash yarn theme={null}
  yarn add @microapp-io/auth
  ```

  ```bash pnpm theme={null}
  pnpm add @microapp-io/auth
  ```
</CodeGroup>

### 2. Import and Initialize the Auth SDK

To use the Auth SDK in your code, import it in any file where you need to use it.

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

const auth = new Auth();
```

For local development, we recommend using the `sandbox` feature on the `Auth` class to return mocked users:

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

const auth = new Auth({
  sandbox: {
    enabled: process.env.NODE_ENV !== 'production',
    user: { id: '1', email: 'hi@microapp.io', pictureUrl: 'https://example.com/avatar.png' },
  },
});
```

<Warning>
  The `sandbox` prop should only be used for local development. Do not use it in production.
</Warning>

### 3. Check if the User is Authenticated

The `isAuthenticated` method returns the user's authentication status: `true` if authenticated and `false` if unauthenticated.

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

const isAuthenticated = await auth.isAuthenticated();

if (isAuthenticated) {
  console.log('User is authenticated');
} else {
  console.log('User is not authenticated');
}
```

### 4. Get the User's Profile Information

The `getUser` method returns the user's profile information.

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

const user = await auth.getUser();

console.log('User Profile:', user);
```

### 3. Prompt the User to Log In

If the user is not authenticated, you can prompt them to authenticate by calling the `requestLogin` method.

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

await auth.requestLogin();
```

### 5. Get Notified when the User is Authenticated

You can get notified when the user authenticates using `onUserAuthenticated` method.

```typescript theme={null}
import { Auth } from '@microapp-io/auth';

const unsubscribeCallback = auth.onUserAuthenticated((user) => {
  console.log(`User just authenticated`, user);
});

await auth.requestLogin();

// Unsubscribe the callback
unsubscribeCallback();
```
