chandralegend's picture
added authentication
698ece2
raw
history blame
No virus
1.8 kB
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})