Waiting a component before executing another component

Hi,

I have 1 chat bot with 2 APIs:

  1. API to get an authentication token from a third-party, I call “AUTHEN”. It was created by using a bi-directional component. That’s because the third-party needs to verify the cookie from the client side
  2. API to conversation. I call “CONVERSATION”

When I leave a message and submit it, the AUTHEN need to be called to get token first, then I push the token to the “CHAT”

What happens:
For the first question, it works. But I leave the second question, the “CHAT” is called first, and it didn’t wait for the token from the AUTHEN call, so the token is None.

How can I wait for the AUTHEN before executing the CHAT?

Simplified code snippet:

from authen_component import authen_component
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

session_id = get_script_run_ctx().session_id

AUTHEN_URL = "https://xxx.xxx.com"

def get_authen_key(key):
    if "authen" not in st.session_state:
        st.session_state.authen = None
    if ENV == PROD:
        if st.session_state.authen == None:
            data = bisso_component(AUTHEN_URL, key=key)
            if data and "success" in data and data["success"]:
                authen_data = {
                    "text": data["text"],
                    "cwid": data["cwid"],
                    "issuer": data["issuer"],
                    "instance_id": data["instanceID"]
                }
                st.session_state.authen = authen_data

get_bisso_key(session_id)

def chat(question):
  authen_key = st.session_state.authen if "authen" in st.session_state else None
  if authen_key:
     headers = {
                "x-authen-token": authen_key.get('token', None),
            }
  res = requests.post(f"{BACKEND_URL}/api/v1/chat",
                        headers=headers,
                        json={
                            "question": question,
                        })
        data = res.json()
        st.markdown(data)
        if "authen" in st.session_state:
            st.session_state.authen = None

def main():
 prompt = st.chat_input("Type a message...")
 if prompt:
        st.empty()
        with st.chat_message("user"):
            st.markdown(prompt)
        with st.spinner("..."):
            chat(
                question=prompt
            )

try:
    main()
except Exception as exc:
    st.error("An error has occurred. Please contact your administrator.")

May you help?
Thank for your attention.