-
Notifications
You must be signed in to change notification settings - Fork 114
Jarvis Authentication System
Welcome to the authentication setup guide for Jarvis AI Assistant.
This guide will help you implement secure user login using Streamlit’s built-in authentication system and an identity provider such as Google.
The streamlit-authentication system is used to authorize the user. Link
To authorize the user, we need an identity provider like google, microsoft, github, linkedin, etc.
An Identity Provider (IdP) manages and verifies user identities. It plays a crucial role in Single Sign-On (SSO) by:
- Authenticating users
- Issuing tokens to grant access to applications
- Enhancing security and simplifying session management
Think of it as a trusted bridge between the client and the server.
Streamlit offers a simple API for handling authentication:
Command | Description |
---|---|
st.login('google') |
Starts login using the specified identity provider. |
st.user |
A dict-like object for accessing the user information. |
st.logout() |
Logout the user and resets their session. |
In Jarvis, Google is used as the default provider.
- Add This to
.streamlit/secrets.toml
[auth]
redirect_uri = "http://localhost:8501/oauth2callback"
cookie_secret = "xxx"
[auth.google]
client_id = "xxx"
client_secret = "xxx"
server_metadata_url = "https://accounts.google.com/.well-known/openid-configuration"
-
redirect_uri
is a url where the auth provider will redirect the user. Don't forget to write the suffix/oauth2callback
in the url. For production url, update the url accordingly. -
cookie_secret
is a cryptographic key used by a web server to sign and verify session cookies. It's a crucial component for maintaining user authentication and session management. -
Go through the google cloud console and create a new project on there.
-
Go to google clients to create the oauth credentials
-
Click on Create client.
-
Select the
Web application
to be application type. -
Give your client a random name or chosen one.
-
Add your authorized URL to redirect the user on successful authorization.
Don't forget to add your production URL, if using your application in production.
- Click on create and save your Client ID & Client Secret or you can download it later.
Add all those credentials in your secrets.toml file inside .streamlit
folder.
Below a small implementation of using your authentication system using python,
import streamlit as st
if st.user and not st.user.is_logged_in:
st.markdown("# Login Required")
if st.button("Log in with Google"):
st.login("google")
else:
st.markdown(f"# Welcome, {st.user.name}!")
st.image(st.user.picture, caption="User Profile Picture")
if st.button("Log out"):
st.logout()
- Always secure your secrets — never commit them to Git!
- Redirect URI must match exactly in both your code and Google settings
- In production, use
https://your-domain.com/oauth2callback