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.