File size: 1,248 Bytes
41b05bb
 
5fb2d40
 
 
 
 
 
41b05bb
5fb2d40
 
41b05bb
5fb2d40
 
 
 
86a454c
5fb2d40
 
 
86a454c
5fb2d40
 
 
 
 
 
 
41b05bb
5fb2d40
 
 
41b05bb
 
 
 
1310b61
 
 
 
 
5fb2d40
 
41b05bb
 
 
5fb2d40
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
# services/auth.py

import streamlit as st
from streamlit_authenticator import Authenticate
from config.settings import settings
from repositories.user_repo import UserRepo
from models.db import init_db

# 1) Ensure DB tables exist
init_db()

# 2) User repo
user_repo = UserRepo(settings.database_url)

def init_auth():
    users = user_repo.get_all_users()
    user_map = {
        u.username: {"name": u.full_name, "password": u.hashed_password}
        for u in users
    }
    creds = {"usernames": user_map}
    return Authenticate(
        credentials=creds,
        cookie_name="quantum_healthcare_auth",
        key=settings.secret_key,
        cookie_expiry_days=1,
    )

# 3) Instantiate authenticator
authenticator = init_auth()

def require_login():
    # πŸ”§ Initialize logout flag so login() can read it
    if 'logout' not in st.session_state:
        st.session_state['logout'] = False

    login_result = authenticator.login(location="sidebar")
    if login_result is None:
        st.stop()

    name, authentication_status, username = login_result
    if not authentication_status:
        st.stop()

    # πŸ›‘ Render the logout button once logged in
    authenticator.logout("Logout", location="sidebar")
    return username