Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions content/develop/api-reference/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1958,11 +1958,11 @@ st.logout()

<h4>User info</h4>

`st.experimental_user` returns information about a logged-in user.
`st.user` returns information about a logged-in user.

```python
if st.experimental_user.is_logged_in:
st.write(f"Welcome back, {st.experimental_user.name}!")
if st.user.is_logged_in:
st.write(f"Welcome back, {st.user.name}!")
```

</RefCard>
Expand Down Expand Up @@ -2234,7 +2234,7 @@ st.query_params.clear()

<h4>Context</h4>

`st.context` provides a read-only interface to access cookies and headers.
`st.context` provides a read-only interface to access cookies, headers, locale, and other browser-session information.

```python
st.context.cookies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Streamlit re-executes your script with each user interaction. Widgets have built

<h4>Context</h4>

`st.context` provides a read-only interface to access cookies and headers.
`st.context` provides a read-only interface to access cookies, headers, locale, and other browser-session information.

```python
st.context.cookies
Expand Down
6 changes: 6 additions & 0 deletions content/develop/api-reference/caching-and-state/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ description: st.context displays a read-only dict of cookies and headers

<Autofunction function="context.headers" />

<Autofunction function="context.ip_address" />

<Autofunction function="context.is_embedded" />

<Autofunction function="context.locale" />

<Autofunction function="context.timezone" />

<Autofunction function="context.timezone_offset" />

<Autofunction function="context.url" />
6 changes: 3 additions & 3 deletions content/develop/api-reference/user/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ st.logout()

<h4>User info</h4>

`st.experimental_user` returns information about a logged-in user.
`st.user` returns information about a logged-in user.

```python
if st.experimental_user.is_logged_in:
st.write(f"Welcome back, {st.experimental_user.name}!")
if st.user.is_logged_in:
st.write(f"Welcome back, {st.user.name}!")
```

</RefCard>
Expand Down
16 changes: 5 additions & 11 deletions content/develop/api-reference/user/user.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
---
title: st.user
slug: /develop/api-reference/user/st.user
description: st.experimental_user returns information about the logged-in user of private apps on Streamlit Community Cloud.
description: st.user returns information about the logged-in user of private apps on Streamlit Community Cloud.
---

<Important>

This is an experimental feature. Experimental features and their APIs may change or be removed at any time. To learn more, click [here](/develop/quick-reference/prerelease#experimental-features).

</Important>

<Autofunction function="streamlit.experimental_user" />
<Autofunction function="streamlit.user" oldName="streamlit.experimental_user" />

### Community Cloud

On Community Cloud, if your app is not configured for authentication, `st.experimental_user` will have a single attribute: `email`. If a user is logged in and a member of your app's workspace, this will return the user's email. For all other cases, it returns `None`.
On Community Cloud, if your app is not configured for authentication, `st.user` will have a single attribute: `email`. If a user is logged in and a member of your app's workspace, this will return the user's email. For all other cases, it returns `None`.

On Community Cloud, if your app is configured for authentication (`[auth]` exists in your app's secrets), `st.experimental_user` will behave the same as a locally running app. Remember to update your identity provider's configuration and your app's secrets to allow your new domain. A list of [IP addresses](/deploy/streamlit-community-cloud/status#ip-addresses) used by Community Cloud is available if needed. An authentication-configured app counts as your one, allowed private app.
On Community Cloud, if your app is configured for authentication (`[auth]` exists in your app's secrets), `st.user` will behave the same as a locally running app. Remember to update your identity provider's configuration and your app's secrets to allow your new domain. A list of [IP addresses](/deploy/streamlit-community-cloud/status#ip-addresses) used by Community Cloud is available if needed. An authentication-configured app counts as your one, allowed private app.

<Autofunction function="streamlit.experimental_user.to_dict" />
<Autofunction function="streamlit.user.to_dict" oldName="streamlit.experimental_user.to_dict" />
24 changes: 12 additions & 12 deletions content/develop/concepts/connections/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ Some popular OIDC providers are:
- [Okta](https://help.okta.com/en-us/content/topics/apps/apps_app_integration_wizard_oidc.htm)
- [Auth0](https://auth0.com/docs/get-started/auth0-overview/create-applications/regular-web-apps)

## `st.login()`, `st.experimental_user`, and `st.logout()`
## `st.login()`, `st.user`, and `st.logout()`

There are three commands involved with user authentication:

- [`st.login()`](/develop/api-reference/user/st.login) redirects the user to your identity provider. After they log in, Streamlit stores an identity cookie and then redirects them to the homepage of your app in a new session.
- [`st.experimental_user`](/develop/api-reference/user/st.user) is a dict-like object for accessing user information. It has a persistent attribute, `.is_logged_in`, which you can check for the user's login status. When they are logged in, other attributes are available per your identity provider's configuration.
- [`st.user`](/develop/api-reference/user/st.user) is a dict-like object for accessing user information. It has a persistent attribute, `.is_logged_in`, which you can check for the user's login status. When they are logged in, other attributes are available per your identity provider's configuration.
- [`st.logout()`](/develop/api-reference/user/st.logout) removes the identity cookie from the user's browser and redirects them to the homepage of your app in a new session.

## User cookies and logging out

Streamlit checks for the identity cookie at the beginning of each new session. If a user logs in to your app in one tab and then opens a new tab, they will automatically be logged in to your app in the new tab. When you call `st.logout()` in a user session, Streamlit removes the identity cookie and starts a new session. This logs the user out from the current session. However, if they were logged in to other sessions already, they will remain logged in within those sessions. The information in `st.experimental_user` is updated at the beginning of a session (which is why `st.login()` and `st.logout()` both start new sessions after saving or deleting the identity cookie).
Streamlit checks for the identity cookie at the beginning of each new session. If a user logs in to your app in one tab and then opens a new tab, they will automatically be logged in to your app in the new tab. When you call `st.logout()` in a user session, Streamlit removes the identity cookie and starts a new session. This logs the user out from the current session. However, if they were logged in to other sessions already, they will remain logged in within those sessions. The information in `st.user` is updated at the beginning of a session (which is why `st.login()` and `st.logout()` both start new sessions after saving or deleting the identity cookie).

If a user closes your app without logging out, the identity cookie will expire after 30 days. This expiration time is not configurable and is not tied to any expiration time that may be returned in your user's identity token. If you need to prevent persistent authentication in your app, check the expiration information returned by the identity provider in `st.experimental_user` and manually call `st.logout()` when needed.
If a user closes your app without logging out, the identity cookie will expire after 30 days. This expiration time is not configurable and is not tied to any expiration time that may be returned in your user's identity token. If you need to prevent persistent authentication in your app, check the expiration information returned by the identity provider in `st.user` and manually call `st.logout()` when needed.

Streamlit does not modify or delete any cookies saved directly by your identity provider. For example, if you use Google as your identity provider and a user logs in to your app with Google, they will remain logged in to their Google account after they log out of your app with `st.logout()`.

Expand Down Expand Up @@ -96,27 +96,27 @@ In your app, create a simple login flow:
```python
import streamlit as st

if not st.experimental_user.is_logged_in:
if not st.user.is_logged_in:
if st.button("Log in with Google"):
st.login()
st.stop()

if st.button("Log out"):
st.logout()
st.markdown(f"Welcome! {st.experimental_user.name}")
st.markdown(f"Welcome! {st.user.name}")
```

When you use `st.stop()`, your script run ends as soon as the login button is displayed. This lets you avoid nesting your entire page within a conditional block. Additionally, you can use callbacks to simplify the code further:

```python
import streamlit as st

if not st.experimental_user.is_logged_in:
if not st.user.is_logged_in:
st.button("Log in with Google", on_click=st.login)
st.stop()

st.button("Log out", on_click=st.logout)
st.markdown(f"Welcome! {st.experimental_user.name}")
st.markdown(f"Welcome! {st.user.name}")
```

## Using multiple OIDC providers
Expand Down Expand Up @@ -152,7 +152,7 @@ Your app code:
```python
import streamlit as st

if not st.experimental_user.is_logged_in:
if not st.user.is_logged_in:
if st.button("Log in with Google"):
st.login("google")
if st.button("Log in with Microsoft"):
Expand All @@ -161,21 +161,21 @@ if not st.experimental_user.is_logged_in:

if st.button("Log out"):
st.logout()
st.markdown(f"Welcome! {st.experimental_user.name}")
st.markdown(f"Welcome! {st.user.name}")
```

Using callbacks, this would look like:

```python
import streamlit as st

if not st.experimental_user.is_logged_in:
if not st.user.is_logged_in:
st.button("Log in with Google", on_click=st.login, args=["google"])
st.button("Log in with Microsoft", on_click=st.login, args=["microsoft"])
st.stop()

st.button("Log out", on_click=st.logout)
st.markdown(f"Welcome! {st.experimental_user.name}")
st.markdown(f"Welcome! {st.user.name}")
```

## Passing keywords to your identity provider
Expand Down
11 changes: 7 additions & 4 deletions content/develop/quick-references/api-cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ slug: /develop/quick-reference/cheat-sheet

# Streamlit API cheat sheet

This is a summary of the docs for the latest version of Streamlit, [v1.44.0](https://pypi.org/project/streamlit/1.44.0/).
This is a summary of the docs for the latest version of Streamlit, [v1.45.0](https://pypi.org/project/streamlit/1.45.0/).

<Masonry>

Expand Down Expand Up @@ -513,17 +513,20 @@ st.exception(e)

```python
# Authenticate users
if not st.experimental_user.is_logged_in:
if not st.user.is_logged_in:
st.login("my_provider")
f"Hi, {st.experimental_user.name}"
f"Hi, {st.user.name}"
st.logout()

# Get dictionaries of cookies and headers
# Get dictionaries of cookies, headers, locale, and browser data
st.context.cookies
st.context.headers
st.context.ip_address
st.context.is_embedded
st.context.locale
st.context.timezone
st.context.timezone_offset
st.context.url
```

</CodeTile>
Expand Down
Loading