Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
hugozhu committed Sep 26, 2023
1 parent b4e5163 commit a6db428
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools", "wheel"]

[project]
name = "streamlit-auth"
version = "0.1.6"
version = "0.1.7"
description = "A Python package for creating oauth protected Streamlit apps"
authors = [
{name = "Hugo Zhu", email = "hugozhu@gmail.com"}
Expand Down
33 changes: 17 additions & 16 deletions src/streamlit_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,28 @@ def require_auth(message="Please sign in"):
st.stop()

def add_auth(required=True, show_login_button=True, show_sidebar=True):
user_info = get_logged_in_user()
if show_login_button:
if not user_info:
if oauth_provider == 'keycloak':
show_keycloak_login_button()
if oauth_provider == 'google':
show_google_login_button()
st.stop()

if show_sidebar:
st.sidebar.write(user_info['email'])
user_info = get_logged_in_user()
stb = st.sidebar if show_sidebar else st._main
with stb:
if show_login_button:
if not user_info:
if oauth_provider == 'keycloak':
show_keycloak_login_button(sidebar = show_sidebar)
if oauth_provider == 'google':
show_google_login_button(sidebar = show_sidebar)
st.stop()

st.write(user_info['email'])
if oauth_provider == 'keycloak':
if st.sidebar.button("Logout", type="primary"):
if st.button("Logout", type="primary"):
uuid = cookie_manager.get("uuid")
if uuid is not None:
uuid = ""
else:
cookie_manager.delete("uuid")
st.session_state.pop('uuid', None)
st.cache_data.login_users.pop(uuid, None)
st.sidebar.markdown(f"""
st.markdown(f"""
<a id="keycloak-btn" href="javascript:void(0);"></a>
""", unsafe_allow_html=True)
from streamlit.components.v1 import html
Expand All @@ -100,7 +101,7 @@ def add_auth(required=True, show_login_button=True, show_sidebar=True):
""")

if oauth_provider == 'google':
if st.sidebar.button("Logout", type="primary"):
if st.button("Logout", type="primary"):
uuid = get_sssion_id()
if uuid is None:
uuid = ""
Expand All @@ -125,9 +126,9 @@ def get_logged_in_user() -> Optional[Dict]:
if oauth_provider == 'google':
try:
user_info = get_google_user()
logger.info("--google -%s---", user_info)
# logger.info("--google -%s---", user_info)
except:
logger.info("parse google callback url error")
# logger.info("parse google callback url error")
user_info = None

if user_info:
Expand Down
4 changes: 2 additions & 2 deletions src/streamlit_auth/google_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def get_access_token_from_query_params(
return token


def show_login_button():
def show_login_button(sidebar: bool = True):
authorization_url = asyncio.run(
get_authorization_url(client=client, redirect_url=redirect_url)
)
markdown_button(authorization_url, "Login with Google")
markdown_button(authorization_url, "Login with Google", sidebar=sidebar)


def get_logged_in_user() -> Optional[Dict]:
Expand Down
4 changes: 2 additions & 2 deletions src/streamlit_auth/keycloak_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ def markdown_label(
unsafe_allow_html=True,
)

def show_login_button():
markdown_label("Please sign in ...")
def show_login_button(sidebar: bool = True):
markdown_label("Please sign in ...", sidebar=sidebar)
2 changes: 1 addition & 1 deletion streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from streamlit_auth import require_auth, add_auth

auth = add_auth()
auth = add_auth(True, True, False)

st.write(auth)

Expand Down

0 comments on commit a6db428

Please sign in to comment.