File size: 1,798 Bytes
8210d08
 
698ece2
 
 
 
 
8210d08
 
 
 
 
 
 
698ece2
8210d08
698ece2
8210d08
 
 
 
 
 
 
 
 
 
 
 
dd352c7
8210d08
 
698ece2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8210d08
 
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
import streamlit as st
import os
import requests
import os
import json

# Fetch the service account key JSON file contents


def initialize_login():
    if "login" not in st.session_state:
        st.columns(3)[1].image("assets/logo.png")
        username = st.text_input("Username")
        password = st.text_input("Password", type="password")

        if st.button("Login"):
            authorized = authenticate(username, password)
            if authorized["status"]:
                st.session_state["login"] = authorized
                os.makedirs(
                    os.path.join(".sessions", st.session_state["login"]["username"]),
                    exist_ok=True,
                )
                st.success("Login Successful!")
                st.experimental_rerun()
            else:
                st.error("Invalid username or password")
    else:
        st.sidebar.success(f'Hello, {st.session_state["login"]["Name"]}!')
        st.sidebar.image("assets/logo.png", use_column_width=True)


def authenticate(username, password):
    FIREBASE_WEB_API_KEY = os.environ.get("FIREBASE_WEB_API_KEY")
    payload = json.dumps(
        {
            "email": f"{username}@aieye.com",
            "password": password,
            "returnSecureToken": False,
        }
    )

    rest_api_url = (
        f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"
    )
    r = requests.post(rest_api_url, params={"key": FIREBASE_WEB_API_KEY}, data=payload)
    print(r.json())

    if r.status_code == 200:
        return {
            "status": True,
            "Name": " ".join(username.split("_")),
            "username": username,
        }
    else:
        return {"status": False}


def get_login():
    return st.session_state.get("login", {"status": False})