- Retrieve authorized data as context for a RAG pipeline.
- Use Auth0 FGA to determine if the user has authorization for the data.
Pick your tech stack
Prerequisites
Before getting started, make sure you:1
Create an Auth0 Account
To continue with this quickstart, you need to have an Auth0 account.
2
Create an Auth0 Application
Go to your Auth0 Dashboard to create a new Auth0 Application.
- Navigate to Applications > Applications in the left sidebar.
- Click the Create Application button in the top right.
- In the pop-up select Regular Web Applications and click Create.
- Once the Application is created, switch to the Settings tab.
- Scroll down to the Application URIs section.
- Set Allowed Callback URLs as:
http://localhost:3000/auth/callback
- Set Allowed Logout URLs as:
http://localhost:3000
- Click Save in the bottom right to save your changes.
3
Create an Auth0 FGA account
You need an Auth0 FGA account to complete this quickstart.
4
OpenAI Platform
Set up an OpenAI account and API key.
Prepare Next.js app
Recommended: To use a starter template, clone the Auth0 AI samples repository:Install dependencies
In the root directory of your project, install the following dependencies:@auth0/ai-langchain
: Auth0 AI SDK for LangChain built for GenAI applications powered by LangChain.@langchain/langgraph
: For building stateful, multi-actor applications with LLMs.langchain
: The LangChain library.@langchain/core
: Core LangChain dependencies.@langchain/openai
: OpenAI provider for LangChain.zod
: TypeScript-first schema validation library.langgraph-nextjs-api-passthrough
: API passthrough for LangGraph.
Update the environment file
Copy the.env.example
file to .env.local
and update the variables with your Auth0 credentials. You can find your Auth0 domain, client ID, and client secret in the application you created in the Auth0 Dashboard.Set up an FGA Store
In the Auth0 FGA dashboard:1
Navigate to Settings. In the Authorized Clients section, click +
Create Client.
2
Give your client a name and mark all the client permissions that are
required for your use case. For the quickstart, you’ll only need Read and
query.
3
Click Create.

.env.local
file with the following content to the root directory of the project. Click Continue to see the FGA_API_URL
and FGA_API_AUDIENCE.
The confirmation dialog will provide you with all the information that you need for your environment file..env.local
Secure the RAG Tool
After configuring your FGA Store, secure the RAG tool using Auth0 FGA and Auth0 AI SDK.The starter application is already configured to handle documents and embeddings.Document Upload and Storage- You can upload documents through the UI (
src/app/documents/page.tsx
). - Uploaded documents are processed by the API route (
src/app/api/documents/upload/route.ts
). - APIs for uploading and retrieving documents are defined in (
src/lib/actions/documents.ts
). - Database is defined in
src/lib/db
. - FGA helpers are defined in
src/lib/fga
. - Documents are stored as embeddings in a vector database for efficient retrieval (
src/lib/rag/embedding.ts
).
- When a document is uploaded, the app automatically creates FGA tuples to define which users can access which documents. A tuple signifies a user’s relation to a given object. For example, the below tuple implies that all users can view the
<document name>
object. - Navigate to the Tuple Management section to see the tuples being added. If you want to add a tuple manually for a document, click + Add Tuple. Fill in the following information:
- User:
user:*
- Object: select doc and add
<document name>
in the ID field - Relation:
viewer
- User:
Create a RAG tool
Define a RAG tool that uses theFGAFilter
to filter authorized data from the vector database:src/lib/tools/context-docs.ts
Use the RAG tool from AI agent
Call the tool from your AI agent to get data from documents. First, update the/src/app/api/chat/[..._path]/route.ts
file with the following code to pass the user credentials to your agent:src/app/api/chat/[..._path]/route.ts
src/lib/auth0.ts
:src/lib/auth0.ts
/src/lib/agent.ts
file with the following code to add the tool to your agent:src/lib/agent.ts
Test your application
Start the database and create required tables:npm run all:dev
. Then, navigate to http://localhost:3000
.This will open the LangGraph Studio in a new tab. You can close it as we won’t
require it for testing the application.
Next steps
- Authorization for RAG docs
- Learn how to use Auth0 FGA to create a Relationship-Based Access Control (ReBAC) authorization model.
- Learn more about OpenFGA.