Harden login gate: graceful message when auth not configured
Browse filesInstead of letting st.login() raise StreamlitAuthError (raw traceback) when
no [auth] provider is configured, detect configuration and show a clean
"sign-in not set up" notice; also wrap st.login() in try/except.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- auth_gate.py +32 -3
auth_gate.py
CHANGED
|
@@ -101,6 +101,16 @@ _LOGIN_CSS = """
|
|
| 101 |
}
|
| 102 |
.login-foot-note { font-size: 0.68rem; color: #7C726A !important; }
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
@keyframes loginRise {
|
| 105 |
from { opacity: 0; transform: translateY(22px) scale(0.985); }
|
| 106 |
to { opacity: 1; transform: none; }
|
|
@@ -125,6 +135,14 @@ def _is_logged_in() -> bool:
|
|
| 125 |
return False
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
def require_login() -> None:
|
| 129 |
"""Block the app until the user signs in with their PTT (Entra) account.
|
| 130 |
|
|
@@ -170,9 +188,20 @@ def require_login() -> None:
|
|
| 170 |
unsafe_allow_html=True,
|
| 171 |
)
|
| 172 |
|
| 173 |
-
if
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
st.markdown(
|
| 178 |
"""
|
|
|
|
| 101 |
}
|
| 102 |
.login-foot-note { font-size: 0.68rem; color: #7C726A !important; }
|
| 103 |
|
| 104 |
+
/* Shown when no auth provider is configured (graceful instead of a crash) */
|
| 105 |
+
.login-warn {
|
| 106 |
+
margin: 0.2rem 1.7rem 0.2rem; padding: 0.75rem 0.9rem;
|
| 107 |
+
background: rgba(232, 93, 39, 0.12);
|
| 108 |
+
border: 1px solid rgba(232, 93, 39, 0.35);
|
| 109 |
+
border-radius: 12px;
|
| 110 |
+
color: #F0B79A; font-size: 0.82rem; font-weight: 600;
|
| 111 |
+
text-align: center; line-height: 1.5;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
@keyframes loginRise {
|
| 115 |
from { opacity: 0; transform: translateY(22px) scale(0.985); }
|
| 116 |
to { opacity: 1; transform: none; }
|
|
|
|
| 135 |
return False
|
| 136 |
|
| 137 |
|
| 138 |
+
def _auth_configured() -> bool:
|
| 139 |
+
"""True if an [auth] provider is configured in secrets.toml."""
|
| 140 |
+
try:
|
| 141 |
+
return "auth" in st.secrets
|
| 142 |
+
except Exception:
|
| 143 |
+
return False
|
| 144 |
+
|
| 145 |
+
|
| 146 |
def require_login() -> None:
|
| 147 |
"""Block the app until the user signs in with their PTT (Entra) account.
|
| 148 |
|
|
|
|
| 188 |
unsafe_allow_html=True,
|
| 189 |
)
|
| 190 |
|
| 191 |
+
if _auth_configured():
|
| 192 |
+
if st.button(":material/login: เข้าสู่ระบบด้วย Microsoft",
|
| 193 |
+
type="primary", use_container_width=True):
|
| 194 |
+
try:
|
| 195 |
+
st.login()
|
| 196 |
+
except Exception:
|
| 197 |
+
st.error("ไม่สามารถเริ่มการเข้าสู่ระบบได้ "
|
| 198 |
+
"กรุณาลองใหม่หรือติดต่อผู้ดูแลระบบ")
|
| 199 |
+
else:
|
| 200 |
+
st.markdown(
|
| 201 |
+
"<div class='login-warn'>⚠ ระบบเข้าสู่ระบบยังไม่ถูกตั้งค่า<br>"
|
| 202 |
+
"กรุณาติดต่อผู้ดูแลระบบ</div>",
|
| 203 |
+
unsafe_allow_html=True,
|
| 204 |
+
)
|
| 205 |
|
| 206 |
st.markdown(
|
| 207 |
"""
|