Next.js
FastAPI
Prerequisites
Before using this example, make sure you:
- Install Node.js 18+ and
npm. - Set up an OpenAI API key.
- Complete the User authentication quickstart to create a Next.js app integrated with Auth0.
- Set up and configure a Google Cloud project:
- Enable the Google Calendar API.
- Create OAuth 2.0 credentials (Web Application) with proper redirect URIs.
- Configure a Social Connection for Google in Auth0
- Make sure to enable
Token Vault - Select
Offline Accessscope
- Make sure to enable
1. Configure Auth0 AI
First, you must install the SDK:./src/lib/auth0-ai.ts
2. Integrate your tool with Google Calendar
Wrap your tool using the Auth0 AI SDK to obtain an access token for the Google Calendar API../src/lib/tools/checkUsersCalendar.ts
ToolNode. The agent will automatically request the access token when the tool is called../src/lib/agent.ts
3. Handle authentication redirects
Interrupts are a way for the system to pause execution and prompt the user to take an action —such as authenticating or granting API access— before resuming the interaction. This ensures that any required access is granted dynamically and securely during the chat experience. In this context, Auth0-AI SDK manages such authentication redirects integrated with the Langchain SDK.Server Side
On the server side of your Next.js application you need to set up a route to handle the Chat API requests. This route will be responsible for forwarding the requests to the LangGraph API. Additionally, you must provide therefreshToken to the Langchain’s RunnableConfig from the authenticated user’s session../src/app/api/langgraph/[..._path]/route.ts
auth0 is an instance of @auth0/nextjs-auth0 to handle the application auth flows. You can check different authentication options for Next.js with Auth0 at the official documentation. :::
Client Side
On this example we utilize theEnsureAPIAccessPopup component to show a popup that allows the user to authenticate with Google Calendar and grant access with the requested scopes. You’ll first need to install the @auth0/ai-components package:./src/components/chat.tsx
Account Linking
If you're integrating with Google, but users in your app or agent can sign in using other methods (e.g., a username and password or another social provider), you'll need to link these identities into a single user account. Auth0 refers to this process as Account Linking.Account Linking logic and handling will vary depending on your app or agent. You can find an example of how to implement it in a Next.js chatbot app here. If you have questions or are looking for best practices, join our Discord and ask in the#auth0-for-gen-ai channel.