File size: 3,629 Bytes
ce1a6c4
 
10dce96
 
 
ce1a6c4
 
d3efaea
 
 
10dce96
 
d3efaea
10dce96
 
 
 
 
 
 
 
 
 
 
 
 
 
869be3d
 
 
 
 
 
10dce96
 
 
 
 
 
 
 
 
869be3d
 
 
 
 
 
 
 
 
 
 
 
 
10dce96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d7b164
10dce96
869be3d
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
import os
import json
import streamlit as st
import firebase_admin
from firebase_admin import credentials, auth
# Retrieve the JSON string from the secret
json_str = os.getenv("FIREBASE_CREDENTIALS")
# Retrieve the JSON string from the secret
json_str = os.getenv("FIREBASE_CREDENTIALS")
firebase_Creds = json.loads(json_str)
# Initialize Firebase (only once)
if not firebase_admin._apps:
    cred = credentials.Certificate(firebase_Creds)  # Replace with your Firebase service account key
    firebase_admin.initialize_app(cred)

# Initialize session state
if "logged_in" not in st.session_state:
    st.session_state.logged_in = False
if "current_user" not in st.session_state:
    st.session_state.current_user = None

# Callback for registration
def register_callback():
    email = st.session_state.reg_email
    password = st.session_state.reg_password
    try:
        # Create a new user in Firebase
        user = auth.create_user_with_email_and_password(email, password)
        st.success("Registration successful! Please check your email for verification.")

        # Send verification email
        auth.send_email_verification(user['idToken'])
        st.info("A verification email has been sent to your email address.")
    except Exception as e:
        st.error(f"Registration failed: {e}")

# Callback for login
def login_callback():
    email = st.session_state.login_email
    password = st.session_state.login_password
    try:
        # Authenticate user with Firebase
        user = auth.sign_in_with_email_and_password(email, password)
        
        # Check if email is verified
        user_info = auth.get_account_info(user['idToken'])
        if user_info['users'][0]['emailVerified']:
            st.session_state.logged_in = True
            st.session_state.current_user = user['localId']
            st.success("Logged in successfully!")
        else:
            st.error("Please verify your email address before logging in.")
            # Resend verification email if needed
            auth.send_email_verification(user['idToken'])
            st.info("A new verification email has been sent to your email address.")
    except Exception as e:
        st.error(f"Login failed: {e}")

# Callback for logout
def logout_callback():
    st.session_state.logged_in = False
    st.session_state.current_user = None
    st.info("Logged out successfully!")

# Registration form
def registration_form():
    with st.form("Registration"):
        st.subheader("Register")
        email = st.text_input("Email", key="reg_email")
        password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
        submit_button = st.form_submit_button("Register", on_click=register_callback)

# Login form
def login_form():
    with st.form("Login"):
        st.subheader("Login")
        email = st.text_input("Email", key="login_email")
        password = st.text_input("Password", type="password", key="login_password")
        submit_button = st.form_submit_button("Login", on_click=login_callback)

# Main app screen (after login)
def main_app():
    st.subheader(f"Welcome, {st.session_state.current_user}!")
    st.write("Enter a name below to get a greeting.")

    # Input field for name
    name = st.text_input("Enter a name", key="name_input")

    # Display greeting in real-time
    if name:
        st.write(f"Hello {name}!")

    # Logout button
    if st.button("Logout", on_click=logout_callback):
        pass

# Render the appropriate screen based on login status
if st.session_state.logged_in:
    main_app()
else:
    registration_form()
    login_form()