Skip to main content

Accessing Authenticated User Information

When you enable the login_required option in your MINEO Live App, you can access authenticated user information through HTTP headers. These headers are automatically sent to your Streamlit application when a user logs in.

Available Headers

MINEO sends the following headers (when available in the user's profile):

HeaderDescriptionExample
X-User-UUIDUser's unique identifier550e8400-e29b-41d4-a716-446655440000
X-User-UsernameUsernamejohndoe
X-User-EmailEmail addressjohn@example.com
X-User-First-NameUser's first nameJohn
X-User-Last-NameUser's last nameDoe
X-User-GroupsUser's groups serialized as json

How to Access Headers in Streamlit

You can access these headers using Streamlit's st.context.headers method. This is the recommended approach for newer versions of Streamlit.

Example 1: Get User as Dictionary

import streamlit as st

def get_authenticated_user():
"""Get authenticated user from HTTP headers"""
try:
headers = st.context.headers
user = {
'uuid': headers.get("X-User-UUID"),
'username': headers.get("X-User-Username"),
'email': headers.get("X-User-Email"),
'first_name': headers.get("X-User-First-Name"),
'last_name': headers.get("X-User-Last-Name"),
'groups': headers.get("X-User-Groups")
}
# Return user if UUID exists (indicates authentication)
return user if user['uuid'] else None
except Exception as e:
st.error(f"Error getting user: {e}")
return None

def main():
st.title("MINEO Authenticated App")

user = get_authenticated_user()
if user:
st.success(f"Welcome, {user.get('first_name', 'User')}!")
st.json(user)
else:
st.warning("No authenticated user or app doesn't have 'login_required' enabled")

if __name__ == "__main__":
main()

Example 2: Access Individual Headers

import streamlit as st

def get_auth_headers():
"""Get all authentication headers"""
try:
headers = st.context.headers
return {
'uuid': headers.get("X-User-UUID"),
'username': headers.get("X-User-Username"),
'email': headers.get("X-User-Email"),
'first_name': headers.get("X-User-First-Name"),
'last_name': headers.get("X-User-Last-Name"),
'groups': headers.get("X-User-Groups")
}
except:
return None

def main():
st.title("Personalized Dashboard")

user_info = get_auth_headers()

if user_info and user_info['uuid']:
# Personalize app based on user
st.sidebar.write(f"👤 {user_info['first_name']} {user_info['last_name']}")
st.sidebar.write(f"📧 {user_info['email']}")

st.write(f"### Hello, {user_info['first_name']}!")
st.write("This is your personalized application.")

# Example: show user-specific data
user_id = user_info['uuid']
st.info(f"Your user ID is: `{user_id}`")

else:
st.error("🔒 This application requires authentication")
st.write("Please log in to access the content.")

if __name__ == "__main__":
main()

Example 3: Reusable Helper Function

Create an auth_helper.py file in your project:

# auth_helper.py
import streamlit as st
import json

class MineoAuth:
@staticmethod
def get_user():
"""Return authenticated user as dictionary"""
try:
headers = st.context.headers
return {
'uuid': headers.get("X-User-UUID"),
'username': headers.get("X-User-Username"),
'email': headers.get("X-User-Email"),
'first_name': headers.get("X-User-First-Name"),
'last_name': headers.get("X-User-Last-Name"),
'groups': json.loads(headers.get("X-User-Groups"))
}
except:
return None

@staticmethod
def is_authenticated():
"""Check if there's an authenticated user"""
user = MineoAuth.get_user()
return user and user.get('uuid') is not None

Use it in your app.py:

# app.py
import streamlit as st
from auth_helper import MineoAuth

def main():
st.title("Protected App with MINEO Auth")

user = MineoAuth.get_user()

if MineoAuth.is_authenticated():
st.success(f"✅ Authenticated as: {user['email']}")

# Protected content
st.write("## Exclusive content for authenticated users")
st.write(f"Welcome to your personal area, {user['first_name']}!")

# Your business logic here...

else:
st.error("🔐 Access denied")
st.write("This application requires you to log in with your MINEO account.")

if __name__ == "__main__":
main()

Required Configuration

For these headers to be available:

  1. Enable login_required in your MINEO Live App configuration
  2. Headers are only available when the user is logged into MINEO

Important Notes

  • Use st.context.headers to access HTTP headers in Streamlit
  • The deprecated _get_websocket_headers() function should not be used in new applications
  • Headers may be empty if the user is not authenticated
  • Ensure you're using Streamlit version 1.28.0 or higher for st.context.headers

Troubleshooting

If you don't receive the headers:

  1. Verify that login_required is enabled in MINEO
  2. Confirm you're accessing the app through MINEO's domain
  3. Check the Streamlit console for errors
  4. Ensure you're using Streamlit version 1.28.0 or higher
  5. Verify that the user is logged into MINEO

With these examples, you can create Streamlit applications that dynamically adapt to each authenticated user on MINEO.