-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
165 lines (132 loc) · 6.01 KB
/
account.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import firebase_admin
import requests
import streamlit as st
from firebase_admin import auth, credentials, firestore
cred = credentials.Certificate("breast-care-f8bfb-bc242f36019b.json")
firebase_admin.initialize_app(cred)
def app():
st.title('Welcome to :violet[Breast Care] :breastcancer:')
if 'username' not in st.session_state:
st.session_state.username = ''
if 'useremail' not in st.session_state:
st.session_state.useremail = ''
def sign_up_with_email_and_password(email, password, username=None, return_secure_token=True):
try:
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signUp"
payload = {
"email": email,
"password": password,
"returnSecureToken": return_secure_token
}
if username:
payload["displayName"] = username
payload = json.dumps(payload)
r = requests.post(rest_api_url, params={"key": "AIzaSyCSavMsvVtG8Vcp6O3vj660qqSVsufJFBc"}, data=payload)
try:
return r.json()['email']
except:
st.warning(r.json())
except Exception as e:
st.warning(f'Signup failed: {e}')
def sign_in_with_email_and_password(email=None, password=None, return_secure_token=True):
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"
try:
payload = {
"returnSecureToken": return_secure_token
}
if email:
payload["email"] = email
if password:
payload["password"] = password
payload = json.dumps(payload)
print('payload sigin',payload)
r = requests.post(rest_api_url, params={"key": "AIzaSyCSavMsvVtG8Vcp6O3vj660qqSVsufJFBc"}, data=payload)
try:
data = r.json()
user_info = {
'email': data['email'],
'username': data.get('displayName') # Retrieve username if available
}
return user_info
except:
st.warning(data)
except Exception as e:
st.warning(f'Signin failed: {e}')
def reset_password(email):
try:
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode"
payload = {
"email": email,
"requestType": "PASSWORD_RESET"
}
payload = json.dumps(payload)
r = requests.post(rest_api_url, params={"key": "AIzaSyCSavMsvVtG8Vcp6O3vj660qqSVsufJFBc"}, data=payload)
if r.status_code == 200:
return True, "Reset email Sent"
else:
# Handle error response
error_message = r.json().get('error', {}).get('message')
return False, error_message
except Exception as e:
return False, str(e)
# Example usage
# email = "[email protected]"
def f():
try:
# user = auth.get_user_by_email(email)
# print(user.uid)
# st.session_state.username = user.uid
# st.session_state.useremail = user.email
userinfo = sign_in_with_email_and_password(st.session_state.email_input,st.session_state.password_input)
st.session_state.username = userinfo['username']
st.session_state.useremail = userinfo['email']
global Usernm
Usernm=(userinfo['username'])
st.session_state.signedout = True
st.session_state.signout = True
except:
st.warning('Login Failed')
def t():
st.session_state.signout = False
st.session_state.signedout = False
st.session_state.username = ''
def forget():
email = st.text_input('If forget Email')
if st.button('Send Reset Link'):
print(email)
success, message = reset_password(email)
if success:
st.success("Password reset email sent successfully.")
else:
st.warning(f"Password reset failed: {message}")
if "signedout" not in st.session_state:
st.session_state["signedout"] = False
if 'signout' not in st.session_state:
st.session_state['signout'] = False
if not st.session_state["signedout"]: # only show if the state is False, hence the button has never been clicked
choice = st.selectbox('Login/Signup',['Login','Sign up'])
email = st.text_input('Email Address')
password = st.text_input('Password',type='password')
st.session_state.email_input = email
st.session_state.password_input = password
if choice == 'Sign up':
username = st.text_input("Enter your unique username")
if st.button('Create my account'):
# user = auth.create_user(email = email, password = password,uid=username)
user = sign_up_with_email_and_password(email=email,password=password,username=username)
st.success('Account created successfully!')
st.markdown('Please Login using your email and password')
st.balloons()
else:
# st.button('Login', on_click=f)
st.button('Login', on_click=f)
# if st.button('Forget'):
forget()
# st.button('Forget',on_click=forget)
if st.session_state.signout:
st.text('Name '+st.session_state.username)
st.text('Email id: '+st.session_state.useremail)
st.button('Sign out', on_click=t)
def ap():
st.write('Posts')