Redirecting login page to home page

You might be interested in this post, which shows how you can hide pages until logging in using the st_pages package.

The one piece that is different in your request is that you want to hide the home page after you log in, which you can do by switching hide_pages([]) to hide_pages(["login"]) (or whatever your log in page is called that you want to hide).

Note that this hiding simply happens by CSS, and needs to be done on every page of your app to make it work. For example, you might put this in every page in your app:

(The sleep isn’t necessary, just makes it more obvious what’s happening before the page switch)

import streamlit as st
from time import sleep
from st_pages import hide_pages

hide_pages(["login"])

if st.button("Log out"):
    st.session_state["logged_in"] = False
    st.success("Logged out!")
    sleep(0.5)
    st.switch_page("login.py")

Note also that st.switch_page is part of the official streamlit package, and works slightly differently from hide_pages, which comes from the unofficial st_pages package.

1 Like