@cl.password_auth_callback 从登录表单接收用户名和密码。返回一个 cl.User 对象将认证用户,而返回 None 则会认证失败。

您可以根据您喜欢的任何服务(您自己的数据库、私有的 Google 表格等)验证凭据。

这里适用通常的安全最佳实践,在存储密码之前对其进行哈希处理。

示例

from typing import Optional
import chainlit as cl

@cl.password_auth_callback
def auth_callback(username: str, password: str):
    # Fetch the user matching username from your database
    # and compare the hashed password with the value stored in the database
    if (username, password) == ("admin", "admin"):
        return cl.User(
            identifier="admin", metadata={"role": "admin", "provider": "credentials"}
        )
    else:
        return None