Skip to content

Commit

Permalink
upd: improved login/signup with messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisVLRT committed Jan 17, 2024
1 parent 75b0ef0 commit c12de28
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ def create_access_token(*, data: dict, expires_delta: Optional[timedelta] = None
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
expire = datetime.utcnow() + timedelta(minutes=60)
to_encode.update({"exp": expire})
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
3 changes: 3 additions & 0 deletions frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

assets = Path(__file__).parent / "assets"

st.session_state["login_status_message"] = "" if "login_status_message" not in st.session_state else st.session_state["login_status_message"]
st.session_state["login_status_level"] = "info" if "login_status_level" not in st.session_state else st.session_state["login_status_level"]


if __name__ == "__main__":
st.set_page_config(
Expand Down
17 changes: 14 additions & 3 deletions frontend/lib/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from time import sleep
from typing import Optional
from urllib.parse import urljoin

Expand All @@ -9,7 +10,6 @@

FASTAPI_URL = os.getenv("FASTAPI_URL", "http://localhost:8000/")


def auth() -> Optional[str]:
tab = stx.tab_bar(
data=[
Expand All @@ -31,6 +31,8 @@ def login_form():
with st.form("Login"):
username = st.text_input("Username", key="username")
password = st.text_input("Password", type="password")
if st.session_state["login_status_message"]:
getattr(st, st.session_state["login_status_level"])(st.session_state["login_status_message"])
submit = st.form_submit_button("Log in")

if submit:
Expand All @@ -40,7 +42,8 @@ def login_form():
session = create_session()
session = authenticate_session(session, token)
else:
st.error("Failed authentication")
st.session_state["login_status_level"] = "error"
st.session_state["login_status_message"] = "Username/password combination not found"
st.session_state["session"] = session
st.session_state["email"] = username
st.rerun()
Expand All @@ -50,6 +53,8 @@ def signup_form():
with st.form("Signup"):
username = st.text_input("Username", key="username")
password = st.text_input("Password", type="password")
if st.session_state["login_status_message"]:
getattr(st, st.session_state["login_status_level"])(st.session_state["login_status_message"])
submit = st.form_submit_button("Sign up")

if submit:
Expand All @@ -59,8 +64,14 @@ def signup_form():
token = get_token(username, password)
session = create_session()
auth_session = authenticate_session(session, token)
st.session_state["login_status_level"] = "success"
st.session_state["login_status_message"] = "Success! Account created."
if st.session_state["login_status_message"]:
getattr(st, st.session_state["login_status_level"])(st.session_state["login_status_message"])
sleep(1.5)
else:
st.error("Failed signing up")
st.session_state["login_status_level"] = "error"
st.session_state["login_status_message"] = "Failed signing up"
st.session_state["session"] = auth_session
st.session_state["email"] = username
st.rerun()
Expand Down
2 changes: 2 additions & 0 deletions frontend/lib/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def query(verb: str, url: str, **kwargs):
if response.status_code == 401:
st.session_state["session"] = None
st.session_state["email"] = None
st.session_state["login_status_level"] = "error"
st.session_state["login_status_message"] = "Session expired. Please log in again."
st.rerun()

return response

0 comments on commit c12de28

Please sign in to comment.