Skip to content

Commit d7f918e

Browse files
committed
add an app with authentication
1 parent 16beb76 commit d7f918e

File tree

7 files changed

+179
-1
lines changed

7 files changed

+179
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,5 @@ requirements.txt
165165
generated
166166
tests/test_codegen.py
167167
.streamlit/*
168+
!.streamlit/config.yaml
169+
!.streamlit/secrets.toml.example

.streamlit/config.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
credentials:
2+
usernames:
3+
jsmith:
4+
5+
name: John Smith
6+
password: abc # To be replaced with hashed password
7+
rbriggs:
8+
9+
name: Rebecca Briggs
10+
password: def # To be replaced with hashed password
11+
cookie:
12+
expiry_days: 30
13+
key: random_signature_key # Must be string
14+
name: random_cookie_name
15+
preauthorized:
16+
emails:
17+

.streamlit/secrets.toml.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[auth]
2+
redirect_uri = "http://localhost:8501/oauth2callback"
3+
cookie_secret = "xxx"
4+
5+
[auth.microsoft]
6+
client_id = "your-client-id"
7+
client_secret = "your-client-secret"
8+
server_metadata_url = "https://login.microsoftonline.com/consumers/v2.0/.well-known/openid-configuration"

docs/index.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,25 @@ make codegen
3434

3535
### Authentication
3636

37+
#### Credentials
38+
39+
Run the following command to set up the credentials for the application.
40+
41+
```shell
42+
# Run the application
43+
uv run streamlit run workshop_playwright_python/apps/streamlit_authentication.py
44+
```
45+
46+
Then, type your credentials described in [.streamlit/config.yaml](../.streamlit/config.yaml) and click the "Login" button. (e.g. `jsmith:abc`, `rbriggs:def`)
47+
48+
#### OpenID Connect
49+
3750
To run a frontend application with authentication, you can refer to the following links.
3851

3952
- [User authentication and information](https://docs.streamlit.io/develop/concepts/connections/authentication)
4053
- [Use Microsoft Entra to authenticate users](https://docs.streamlit.io/develop/tutorials/authentication/microsoft)
4154

42-
For example, you can use the following file [.streamlit/secrets.toml](../.streamlit/secrets.toml) to set up authentication with Microsoft Entra ID.
55+
For example, you can use the following file [.streamlit/secrets.toml](../.streamlit/secrets.toml.example) to set up authentication with Microsoft Entra ID.
4356

4457
```toml
4558
[auth]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"playwright>=1.52.0",
1010
"pytest-playwright>=0.7.0",
1111
"streamlit>=1.45.0",
12+
"streamlit-authenticator>=0.4.2",
1213
]
1314

1415
[project.optional-dependencies]

uv.lock

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import streamlit as st
2+
import yaml
3+
from streamlit_authenticator import Authenticate, LoginError
4+
from yaml.loader import SafeLoader
5+
6+
# Loading config file
7+
with open(".streamlit/config.yaml", encoding="utf-8") as file:
8+
config = yaml.load(file, Loader=SafeLoader)
9+
10+
# Creating the authenticator object
11+
authenticator = Authenticate(
12+
credentials=config["credentials"],
13+
cookie_name=config["cookie"]["name"],
14+
cookie_key=config["cookie"]["key"],
15+
cookie_expiry_days=config["cookie"]["expiry_days"],
16+
)
17+
18+
# Creating a login widget
19+
try:
20+
result = authenticator.login()
21+
except LoginError as e:
22+
st.error(e)
23+
24+
if st.session_state["authentication_status"]:
25+
st.write(f"Welcome *{st.session_state['name']}*")
26+
st.title("Some content")
27+
authenticator.logout("Logout", "main")

0 commit comments

Comments
 (0)