import streamlit as st from streamlit_oauth import OAuth2Component import os from yahoo_client import YahooFantasyClient # # Load environment variables from .env file # from dotenv import load_dotenv # load_dotenv() # Set environment variables AUTHORIZE_URL = os.environ.get("AUTHORIZE_URL") TOKEN_URL = os.environ.get("TOKEN_URL") REFRESH_TOKEN_URL = os.environ.get("REFRESH_TOKEN_URL") REVOKE_TOKEN_URL = os.environ.get("REVOKE_TOKEN_URL") CLIENT_ID = os.environ.get("CLIENT_ID") CLIENT_SECRET = os.environ.get("CLIENT_SECRET") REDIRECT_URI = os.environ.get("REDIRECT_URI") SCOPE = os.environ.get("SCOPE") ENABLE_LOGIN = os.environ.get("ENABLE_LOGIN", False) ADMIN_GUID = os.environ.get("ADMIN_GUID") # Create OAuth2Component instance oauth2 = OAuth2Component(CLIENT_ID, CLIENT_SECRET, AUTHORIZE_URL, TOKEN_URL, REFRESH_TOKEN_URL, REVOKE_TOKEN_URL) def is_token_in_session() -> bool: return "token" in st.session_state def get_authorization_button(): if not ENABLE_LOGIN: return # Check if token exists in session state if not is_token_in_session(): # If not, show authorize button result = oauth2.authorize_button("Login", REDIRECT_URI, SCOPE) if result and "token" in result: # If authorization successful, save token in session state st.session_state.token = result.get("token") yahoo_con = YahooFantasyClient(oauth2, st.session_state.token) st.session_state.logged_in_guid = yahoo_con.get_guid_for_logged_in_user() st.session_state.yahoo_client = yahoo_con st.session_state.user_admin = st.session_state.logged_in_guid == ADMIN_GUID st.rerun() else: # # If token exists in session state, allow logout st.session_state["token"] if st.button("Logout"): del st.session_state.token del st.session_state.logged_in_guid del st.session_state.yahoo_client del st.session_state.user_admin st.rerun() # # If token exists in session state, show the token # token = st.session_state["token"] # if st.button("Refresh Token"): # # If refresh token button is clicked, refresh the token # token = oauth2.refresh_token(token) # st.session_state.token = token # st.rerun()