Skip to content

Commit

Permalink
Merge pull request #100 from openclimatefix/add-pwd
Browse files Browse the repository at this point in the history
Add pwd
  • Loading branch information
peterdudfield authored Nov 2, 2023
2 parents efd181f + 21c752b commit 0722f0f
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

logger = logging.getLogger(__name__)


def password_entered():
"""Checks whether a password entered by the user is correct."""
if st.session_state["password"] == st.secrets["password"]:
st.session_state["password_correct"] = True
del st.session_state["password"] # don't store password
else:
st.session_state["password_correct"] = False


def check_password():
"""Returns `True` if the user had the correct password."""

Expand All @@ -24,24 +34,55 @@ def check_password():
unsafe_allow_html=True,
)

auth0_logged = False
password_logged = False

# check if we have logged on with a password, if we havent show auth0
if ("password_correct" not in st.session_state) or (not st.session_state["password_correct"]):
show_auth0 = True
else:
show_auth0 = False

with col2:
try:
user_info = login_button(clientId=clientId, domain=domain,debug_logs=True)
except Exception as e:
st.text('Could not run auth')
logger.error(f'Could not run auth {e}')
return False
if show_auth0:
# show auth0 log in
try:
user_info = login_button(clientId=clientId, domain=domain,debug_logs=True)
except Exception as e:
st.text('Could not run auth')
logger.error(f'Could not run auth {e}')

if user_info is None:
st.text('No user info')
return False
if user_info is None:
st.text('No user info')

if not user_info:
st.text('Please log in')
return False
if user_info:
if '@openclimatefix.' not in user_info['email']:
st.text('This is only available to OCF members')
else:
auth0_logged = True

if '@openclimatefix.' not in user_info['email']:
st.text('This is only available to OCF members')
return False
# if we have not logged in with auth0
if not auth0_logged:
if "password_correct" not in st.session_state:
# First run, show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password", autocomplete="current-password"
)

elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input(
"Password", type="password", on_change=password_entered, key="password", autocomplete="current-password"
)
st.error("😕 Password incorrect")

return user_info
else:
# Password correct, show success message.
st.success("🔒 Password correct")
password_logged = True

if auth0_logged or password_logged:
return True
else:
st.text('Please log in')
return False

0 comments on commit 0722f0f

Please sign in to comment.