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