Update services/auth.py
Browse files- services/auth.py +33 -28
services/auth.py
CHANGED
@@ -1,28 +1,33 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_authenticator import Authenticate
|
3 |
+
from config.settings import settings
|
4 |
+
from repositories.user_repo import UserRepo
|
5 |
+
from models.db import init_db
|
6 |
+
|
7 |
+
# Ensure the DB tables exist before we fetch users
|
8 |
+
init_db()
|
9 |
+
|
10 |
+
# Instantiate your user repository
|
11 |
+
user_repo = UserRepo(settings.database_url)
|
12 |
+
|
13 |
+
def init_auth():
|
14 |
+
users = user_repo.get_all_users()
|
15 |
+
creds = {
|
16 |
+
u.username: {"name": u.full_name, "password": u.hashed_password}
|
17 |
+
for u in users
|
18 |
+
}
|
19 |
+
return Authenticate(
|
20 |
+
credentials=creds,
|
21 |
+
cookie_name="quantum_healthcare_auth",
|
22 |
+
key=settings.secret_key,
|
23 |
+
cookie_expiry_days=1,
|
24 |
+
)
|
25 |
+
|
26 |
+
# Create the authenticator at import time
|
27 |
+
authenticator = init_auth()
|
28 |
+
|
29 |
+
def require_login():
|
30 |
+
name, authentication_status, username = authenticator.login("Login", "sidebar")
|
31 |
+
if not authentication_status:
|
32 |
+
st.stop()
|
33 |
+
return username
|