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
- 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
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 = 'localhost:8501'
color = 'blue' # Or 'white'
justify_content = 'center'
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.write(st.session_state['user_info'].get('name'))
if st.button('Log out'):
logout()
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
avatar
- 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 and get_authorization_url
If you want to implement the login in another way, you can use the check_cookies
and get_authorization_url
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
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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6c959b0f93646e3396d7edf565ebe85e49883a2e317a92b0de6a1dcfc42e716 |
|
MD5 | 4595f3a2e1cee1897e01567d6a4e8dad |
|
BLAKE2b-256 | 8b4c53ca10bbf003d278905f73c4e9332a099b7be48af1233eec48ed5af1b2a7 |
Hashes for streamlit_google_auth-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15a6687e1330d0e425d876602d4df5b5b4f34b734346f20824e0db03e13bab03 |
|
MD5 | 3c26b60c2a6635aac9af08ee0e17feb8 |
|
BLAKE2b-256 | d1f3b02643cfd80bf3ec26ba3d6df90eed7ba19be3743597c548191eb629fea6 |