-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
65 lines (56 loc) · 2.24 KB
/
test.py
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
import streamlit as st
import requests
# Firebase Authentication API endpoint
FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCWQRFrZaVPhD--tZs7_IHfdRrpU5PxZDM"
FIREBASE_SIGNUP_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyCWQRFrZaVPhD--tZs7_IHfdRrpU5PxZDM"
def sign_in(email, password):
try:
response = requests.post(FIREBASE_AUTH_URL, json={
'email': email,
'password': password,
'returnSecureToken': True
})
response_data = response.json()
if 'idToken' in response_data:
return response_data['idToken']
else:
st.error(response_data.get('error', {}).get('message', 'Unknown error'))
return None
except Exception as e:
st.error(f"Error during sign-in: {str(e)}")
return None
def sign_up(email, password):
try:
response = requests.post(FIREBASE_SIGNUP_URL, json={
'email': email,
'password': password,
'returnSecureToken': True
})
response_data = response.json()
if 'idToken' in response_data:
return response_data['idToken']
else:
st.error(response_data.get('error', {}).get('message', 'Unknown error'))
return None
except Exception as e:
st.error(f"Error during sign-up: {str(e)}")
return None
def main():
st.title("Firebase Authentication with Email and Password")
choice = st.sidebar.selectbox("Login/Signup", ["Login", "Sign Up"])
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if choice == "Login":
if st.button("Login"):
id_token = sign_in(email, password)
if id_token:
st.success("Logged in successfully!")
st.write(f"Your ID Token: {id_token}")
elif choice == "Sign Up":
if st.button("Sign Up"):
id_token = sign_up(email, password)
if id_token:
st.success("Account created successfully!")
st.write(f"Your ID Token: {id_token}")
if __name__ == "__main__":
main()