> For the complete documentation index, see [llms.txt](https://lemonade.gitbook.io/welcome-to-lemonade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lemonade.gitbook.io/welcome-to-lemonade/auth2.0/integration-guide.md).

# 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();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lemonade.gitbook.io/welcome-to-lemonade/auth2.0/integration-guide.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
