Chainlit / chainlit

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

Google Authentication #661

Closed VasuMavaniT closed 8 months ago

VasuMavaniT commented 8 months ago

I want to add google authentication to my chainlit app. For that I have set the environment variables: OAUTH_GOOGLE_CLIENT_ID and OAUTH_GOOGLE_CLIENT_SECRET. What changes should be made in chainlit application below?

import chainlit as cl import pandas as pd import json import time from typing import Optional, Dict

@cl.oauth_callback def oauth_callback( provider_id: str, token: str, raw_user_data: Dict[str, str], default_user: cl.User, ) -> Optional[cl.User]: if provider_id == "google": if raw_user_data["hd"] == "google.com": return default_user return default_user return None

@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() await cl.Message("Welcome to the USA Spending Bot!").send()

willydouhard commented 8 months ago

This looks kinda ok, did you configure your environment variables like google client id and secret id as described in the docs?

VasuMavaniT commented 8 months ago

- So, I have added these 2 variables inside system environment variables: OAUTH_GOOGLE_CLIENT_ID and OAUTH_GOOGLE_CLIENT_SECRET

- And in chainlit application, I have only this file App.py:

import chainlit as cl import pandas as pd import json import time from typing import Optional, Dict

@cl.oauth_callback def oauth_callback( provider_id: str, token: str, raw_user_data: Dict[str, str], default_user: cl.User, ) -> Optional[cl.User]: return default_user

@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() await cl.Message("Welcome to the USA Spending Bot!").send()

**And I am running the app using the command: chainlit run app.py --port 8004 -w

I am getting this error currently:** File "C:\Users\vasu\Documents\Chainlit APFS Site\Entrypoint error resolver\Chainlit Authentication.\app.py", line 12, in default_user: cl.User, ^^^^^^^ File "C:\Users\vasu\AppData\Local\Programs\Python\Python311\Lib\site-packages\chainlit\utils.py", line 64, in getattr module_path = registry[name]


KeyError: 'User'

**I wanted to know what extra file or function is needed to be included for authentication.** 
willydouhard commented 8 months ago

I think you need to update Chainlit. cl.User is introduced in Chainlit >= 1.0.0

VasuMavaniT commented 8 months ago

Thank you, @willydouhard After updating the version of chainlit to the latest version, it is working for me.