Literal AI is sunsetting October 31st, 2025 - View migration guide
Literal AI Logo
Explain the different options for authenticating users in Chainlit.
RAG Agent
(9s)
gpt-4o-2024-05-13
(1.6s)
343 tks
Documentation Retrieval
(0.8s)
Embed
(0.3s)
Retrieve
(0.4s)
Cookbooks Retrieval
(1.1s)
Embed
(0.8s)
Retrieve
(0.3s)
gpt-4o-2024-05-13
(6.2s)
7.67K tks
Chainlit offers several options for authenticating users. Here are the available methods: 1. **Password Authentication**: Authenticate users with a login/password combination. ```python @cl.on_auth async def password_auth(username: str, password: str): if username == "admin" and password == "password": return cl.User(identifier=username) return None ``` 2. **OAuth Authentication**: Authenticate users using OAuth providers like Google. ```python @cl.on_auth async def oauth_auth(token: str): # Verify the token with your OAuth provider user_info = verify_oauth_token(token) if user_info: return cl.User(identifier=user_info["email"]) return None ``` 3. **Header Authentication**: Authenticate users based on a custom header. ```python @cl.on_auth async def header_auth(headers: dict): token = headers.get("Authorization") if token == "your_custom_token": return cl.User(identifier="custom_user") return None ``` To enable authentication, you need to define a `CHAINLIT_AUTH_SECRET` environment variable and add one or more authentication callbacks to your app. ```python import chainlit as cl @cl.on_chat_start async def on_chat_start(): app_user = cl.user_session.get("user") await cl.Message(f"Hello {app_user.identifier}").send() ``` Make sure each user has a unique identifier to prevent them from sharing their data.
Skipping 02:40m
verify_oauth_token?
RAG Agent
(7.8s)
gpt-4o-2024-05-13
(1.7s)
8.05K tks
Cookbooks Retrieval
(1.2s)
Embed
(0.3s)
Retrieve
(0.9s)
gpt-4o-2024-05-13
(4.8s)
13.6K tks
The `verify_oauth_token` function is a placeholder for custom logic to verify OAuth tokens. Here is an example of how you might implement it: ```python import requests def verify_oauth_token(token: str): # Replace with your OAuth provider's token verification endpoint verification_url = "https://oauth2.googleapis.com/tokeninfo" response = requests.get(verification_url, params={"id_token": token}) if response.status_code == 200: return response.json() # Return user info if token is valid return None # Return None if token is invalid ``` You can then use this function in your OAuth authentication callback: ```python @cl.on_auth async def oauth_auth(token: str): user_info = verify_oauth_token(token) if user_info: return cl.User(identifier=user_info["email"]) return None ``` This example uses Google's OAuth token verification endpoint. You should replace the `verification_url` with the appropriate endpoint for your OAuth provider.
User
Jun 1, 2024 1:45:47 PM
Input
Output
Metadata