Use Google authentification in streamlit
Project description
streamlit-google-auth
streamlit-google-auth
is a simple package to add google authentification in your streamlit app.
For that I use the google API, so you will need a google cloud account and to create credentials.
1. Get your google credentials json
- Go to google cloud
- Create an account and a project
- Go to APIs & services
- On the left sidebar, go to Credentials
- Anable the API
- On the top bar, click on + CREATE CREDENTIALS and select OAuth client ID
- Select Web application and name the it as you want
- Add the URLs of your apps in both origins and redirect (Recommend adding
http:localhost:8501
for dev) - You should see a new row in OAuth 2.0 Client IDs where you can download the json
2. Install the package
pip install streamlit-google-auth
3. login and logout
The login
function will first check the cookies if the user already exist.
And create a button that the user can use.
In the other hand, logout
is just a function and a button need to be created.
import streamlit as st
from streamlit_google_auth import login, logout
secret_credentials_path = 'google_credentials.json'
cookie_name = 'my_cookie_name'
cookie_key = 'this_is_secret'
cookie_expiry_days = 30
app_url = 'http://localhost:8501'
color = 'blue' # Or 'white'
justify_content = 'center'
st.title('Streamlit Google Auth Example 1')
login(
secret_credentials_path=secret_credentials_path,
cookie_name=cookie_name,
cookie_key=cookie_key,
cookie_expiry_days=cookie_expiry_days,
app_url=app_url,
color=color,
justify_content=justify_content
)
if st.session_state['connected']:
st.image(st.session_state['user_info'].get('picture'))
st.write('Hello, '+ st.session_state['user_info'].get('name'))
st.write('Your email is '+ st.session_state['user_info'].get('email'))
if st.button('Log out'):
logout(cookie_name)
Once the login function run, session_state will have 3 new keys , and ``.
connected
: A Boolean True if connected, False if notoauth_id
: Is a unique identifier from Googleuser_info
: Is user infos provided by google:name
email
picture
- Note: you can request more when creating the google credentials json. The user will se what you are asking in the google login page.
4. check_cookies, get_authorization_url and check_authentification
If you want to implement the login in another way, you can use the check_cookies
, get_authorization_url
and check_authentification
functions.
At the start of you app, you can call check_cookies
and if the cookie exist, the user is automatically log.
check_cookies
also add the keys connected
, oauth_id
and user_info
to session_state.
from streamlit_google_auth import check_cookies
check_cookies(cookie_name, cookie_key)
And if you just want the url that the user need to use to access the login google url.
This is usefull if you want to do a custom button, ect.
from streamlit_google_auth import get_authorization_url
get_authorization_url(secret_credentials_path, app_url)
Note: You can check for cookies before calling login
Now the last function to use is check_authentification
.
This function will catch when the user come back from the google authentification URL and log the user in.
from streamlit_google_auth import check_authentification
check_authentification(secret_credentials_path, cookie_name, cookie_key, cookie_expiry_days, app_url)
Here a simple example implemented:
import streamlit as st
from streamlit_google_auth import logout, check_cookies, get_authorization_url, check_authentification
secret_credentials_path = 'google_credentials.json'
cookie_name = 'my_cookie_name'
cookie_key = 'this_is_secret'
cookie_expiry_days = 30
app_url = 'http://localhost:8501'
# Check the cookies for authentication
st.title('Streamlit Google Auth Example 2')
if not check_cookies(cookie_name, cookie_key):
check_authentification(secret_credentials_path, cookie_name, cookie_key, cookie_expiry_days, app_url)
if st.session_state['connected']:
st.image(st.session_state['user_info'].get('picture'))
st.write('Hello, '+ st.session_state['user_info'].get('name'))
st.write('Your email is '+ st.session_state['user_info'].get('email'))
if st.button('Log out'):
logout(cookie_name)
else:
st.write('You are not connected')
authorization_url = get_authorization_url(secret_credentials_path, app_url)
st.markdown(f'[Login]({authorization_url})')
st.link_button('Login', authorization_url)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for streamlit-google-auth-1.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15efc6b06977da226c83338ca7f6e988684d84d2cf97ac6e6ea991b2dad51dac |
|
MD5 | 3ccfa74ed46821d97d88cc9acfcdfeca |
|
BLAKE2b-256 | 8ab32f595fc13afce1dfe3d0b3714f3dad97162dbfc3c47941b28943eb1dafc0 |
Hashes for streamlit_google_auth-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81e628728ff887638fc77775590a9b8211064692387d0bbcd9c11373965a2438 |
|
MD5 | ea4062c29005c3dfade1a92d142ebbf0 |
|
BLAKE2b-256 | bd7dfa8bf9f3651815d232f8f43d9635940a11b0912374aa4310e17b8c3d7e3a |