chat-faq / pages /login.py
AnnasBlackHat's picture
using state
6358d4a
import streamlit as st
import extra_streamlit_components as stx
from dependency import get_manager
from dependency import db
from passlib.hash import pbkdf2_sha256
import bcrypt
admin_collection = db['admin']
# Set a cookie with the specified parameters
st.session_state.cookie = "value"
st.session_state.cookie_expires = 3600 # Expires in 1 hour
st.session_state.cookie_domain = "share.streamlit.io"
st.session_state.cookie_secure = True
st.session_state.cookie_path = "/"
# Replace this with your actual authentication logic
def authenticate(email, password):
# Query the admin_collection with the provided email
admin = admin_collection.find_one({"email": email})
# If the email is not found, return False
if not admin:
return False
if bcrypt.checkpw(password.encode('utf-8'), admin["password"].encode('utf-8')):
return True
else:
return False
# Verify the provided password against the hashed password in the database
if pbkdf2_sha256.verify(password, admin["password"]):
return True
else:
return False
cookie_manager = get_manager()
auth_state = cookie_manager.get(cookie='auth_state')
def login():
st.title("Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
if authenticate(username, password):
st.success("Login successful!")
cookie_manager.set('auth_state', 'authenticated')
st.session_state.auth_state = True
st.switch_page('pages/manage_faqs.py')
else:
st.error("Invalid username or password")
if not auth_state == "authenticated":
login()
else:
st.warning("You are already logged in. Redirecting to FAQ management page.")
st.experimental_set_query_params(page="manage_faqs")