-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (65 loc) · 2.81 KB
/
main.py
File metadata and controls
74 lines (65 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
import requests
from url import BASE_URL
# --- CONFIG ---
st.set_page_config(page_title="CryptoSim Auth", layout="centered", page_icon="🔐")
st.markdown("<h1 style='text-align: center;'>CryptoSim</h1>", unsafe_allow_html=True)
# --- TABS ---
tab1, tab2 = st.tabs(["Register", "Login"])
# --- LOGIN TAB ---
with tab2:
st.subheader("Login to Your Account")
identifier = st.text_input("Username or Email", key="login_user")
password = st.text_input("Password", type="password", key="login_pass")
st.write('')
if st.button("Login", type='primary', use_container_width=True):
if not identifier or not password:
st.error("Please fill in all fields.")
else:
res = requests.post(f"{BASE_URL}/login", params={
"identifier": identifier,
"password": password
})
if res.status_code == 200 and res.json().get("success"):
data = res.json()
# ✅ Save token and user info
st.session_state["token"] = data["token"]
st.session_state["user"] = data["user"]
st.session_state["logged_in"] = True
st.success(f"✅ Login successful! Welcome, {data['user']['name']}")
st.rerun() # 🔁 Needed to trigger page switch
else:
print(res.status_code)
st.error(res.json().get("message", "Login failed."))
# --- REGISTER TAB ---
with tab1:
st.subheader("Create a New Account")
name = st.text_input("Full Name")
email = st.text_input("Email")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
age = st.number_input("Age", min_value=18, max_value=100)
phone = st.text_input("Phone Number")
st.write('')
if st.button("Register", type='primary', use_container_width=True):
if not all([name, email, username, password, gender, age, phone]):
st.warning("Please fill out all fields.")
else:
res = requests.post(f"{BASE_URL}/register", params={
"email": email,
"name": name,
"username": username,
"password": password,
"gender": gender,
"age": age,
"phone": phone
})
if res.status_code == 200 and res.json().get("success"):
st.success("🎉 Registered successfully! You can now log in.")
else:
st.write("Status Code:", res.status_code)
st.write("Raw Response:", res.text)
st.error(res.json().get("message", "Registration failed."))
if st.session_state.get("logged_in"):
st.switch_page("pages/dashboard.py")