# Integration Guide

## Overview

This document outlines the steps required to integrate OAuth2 authentication for Lemonade services in your application. It includes acquiring necessary credentials, setting up endpoints, and handling authorization and token refresh flows.

## Prerequisites

Obtain <mark style="color:orange;">`client_id`</mark> and <mark style="color:orange;">`client_secret`</mark> from Lemonade. Note that this process is currently managed internally and does not have a UI interface.

## OAuth2 Endpoints

Lemonade OAuth2 utilizes the following endpoints:

1. **Authorization:** <mark style="color:orange;">`https://oauth2.lemonade.social/oauth2/auth`</mark>
2. **Token:** <mark style="color:orange;">`https://oauth2.lemonade.social/oauth2/token`</mark>
3. **User Information:** <mark style="color:orange;">`https://oauth2.lemonade.social/userinfo`</mark>
4. **End Session:** <mark style="color:orange;">`https://oauth2.lemonade.social/oauth2/sessions/logout`</mark>

## Library Usage

We use [`oidc-client-ts`](https://github.com/authts/oidc-client-ts) for handling the authorization flow and managing session/tokens.

## Configuration

First, import and configure the <mark style="color:orange;">`UserManager`</mark> from <mark style="color:orange;">`oidc-client-ts`</mark>:<br>

```javascript
import { UserManager } from 'oidc-client-ts';

const oauth2Url = 'https://oauth2.lemonade.social'
const userManager = new UserManager({
  automaticSilentRenew: false,
  authority: oauth2Url,
  metadata: {
    authorization_endpoint: `${oauth2Url}oauth2/auth`,
    token_endpoint: `${oauth2Url}oauth2/token`,
    userinfo_endpoint: `${oauth2Url}userinfo`,
    end_session_endpoint: `${oauth2Url}oauth2/sessions/logout`,
  },
  client_id: clientId,
  client_secret: clientSecret,
  redirect_uri: `${window.location.origin}/oauth2/callback`,
});
```

## Authorization Flow

1. **Initiate Login:**

Perform a call to the authorization endpoint:

```javascript
await userManager.signinRedirect({
  scope: 'openid offline_access',
  state: encodeURI(window.location.href),
});
```

This will redirect to Lemonade Identity for user login and consent (currently auto-skipped). After login, it redirects to <mark style="color:orange;">`/oauth2/callback`</mark>.

2. **Handle Callback:**

Process the callback to obtain the user response:

```javascript
const response = await userManager.signinRedirectCallback();
```

<mark style="color:orange;">response</mark> includes access tokens and refresh tokens, which should be stored within your application.

3. **Making Requests:**

* Use the access token to make requests to the Lemonade backend: <https://backend.lemonade.social/graphql>
* Put the access token in the <mark style="color:orange;">`x-ory-kratos-session`</mark> header

## Token Refresh Flow

Access tokens are valid for 1 hour. To refresh:

```javascript
userManager.events.addAccessTokenExpiring(async () => {
  const newUser = await userManager.signinSilent();
});
```

## Log Out Flow

To perform a logout:

```javascript
const user = await userManager.getUser();

userManager.signoutRedirect({
  post_logout_redirect_uri: `${window.location.origin}/oauth2/logout`,
  id_token_hint: user?.id_token,
});

// Process the callback at /oauth2/logout
await userManager.signoutRedirectCallback();
```
