File size: 1,522 Bytes
a5ca742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import firebase_admin
from firebase_admin import credentials, auth
from firebase_admin import initialize_app

def initialize_firebase():
    if not firebase_admin._apps:
        cred = credentials.Certificate(st.secrets["firebase"])
        initialize_app(cred)

def check_authentication():
    initialize_firebase()
    
    if 'user_authenticated' not in st.session_state:
        st.session_state.user_authenticated = False
    
    if not st.session_state.user_authenticated:
        col1, col2 = st.columns(2)
        
        with col1:
            email = st.text_input("Email")
        with col2:
            password = st.text_input("Password", type="password")
            
        col1, col2 = st.columns(2)
        
        with col1:
            if st.button("Login"):
                try:
                    user = auth.get_user_by_email(email)
                    st.session_state.user_authenticated = True
                    st.experimental_rerun()
                except:
                    st.error("Invalid credentials")
                    
        with col2:
            if st.button("Sign Up"):
                try:
                    user = auth.create_user(
                        email=email,
                        password=password
                    )
                    st.success("Account created successfully!")
                except:
                    st.error("Could not create account")
                    
        return False
        
    return True