Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.1k stars 777 forks source link

Automatically creating a user #1015

Open kvnn opened 1 month ago

kvnn commented 1 month ago

Is your feature request related to a problem? Please describe. I don't want to make users create a username / password before using the chat, but I want to provide them a username and password to come back and continue their history. I also want data persistence.

Describe the solution you'd like This is what I'm thinking:

    user = cl.user_session.get("user")
    print(f'user={user}')
    if not user:
        username, password = auto_create_user_and_password()
        user = cl.User(identifier=username, metadata={"role": "user", "provider": "credentials"})
        access_token = create_jwt(user)
        user = await authenticate_user(access_token)
        data_layer = cl_data.get_data_layer()
        await data_layer.create_user(user)

This isn't currently working. I'm seeing:

2024-05-23 00:12:09 - Error while flushing create_element: No authenticated user in context
2024-05-23 00:12:09 - Error while flushing create_step: No authenticated user in context

From the chainlit code, I'm not sure what else the login form does when hitting /login that I'm not doing here. See https://github.com/Chainlit/chainlit/blob/f158482046e632a66d42abe49dfa347402b6f04a/backend/chainlit/server.py#L310

I'd love some advice!

kvnn commented 1 month ago

I get the feeling that it may be due to the front-end not having a token to send on subsequent user messages, etc. I see this in the front-end login form submission: setAccessToken(json.access_token); : https://github.com/Chainlit/chainlit/blob/f158482046e632a66d42abe49dfa347402b6f04a/frontend/src/pages/Login.tsx#L43

kvnn commented 1 month ago

Basically, it would be really nice to be able to just set `cl.user_session('user', MyUser) without all the auth logic butting in.

kvnn commented 1 month ago

So ... maybe I just need to do this in my custom_js : return localStorage.setItem(tokenKey, token);

I'm totally fine just hacking this - if someone can verify that I'm on the right track I'd be grateful.