Login, direct to main page and redirect to login if logout!

Hi developers! Greetings!
In my project I have some users with their roles defined.
I want to create a login page for those users, if credential matches I want to direct streamlit to open the main script file on the same window and removing the login_page.
If user logs out from the main page he will again see the login page.
While logged in I also want to store those credentials for an hour so that in any refresh user dont have to renter the username and password again. Is it possible to do in sytreamlit?
Please help with the best code possible to get this thing done. @ferdy @tonykip @dataprofessor
I was trying to do something with the information that i got on community forum and google but not getting the desired output.

def authenticate(username, password):
    for user in credentials["users"]:
        if user["username"] == username and user["password"] == password:
            return user["role"]
    else:
        return False
def logout():
    del st.session_state.username
    del st.session_state.password
    st.write("Logged out successfully")

@st.cache_data(experimental_allow_widgets=True)
def login_form(a):
    if ("username" not in st.session_state) and ("password" not in st.session_state):
        with st.form("login_form"):
            st.title("Login") 
            username = st.text_input("Username")
            password = st.text_input("Password", type="password")
            submitted = st.form_submit_button("LogIn")
            if submitted:
                role = authenticate(username,password)
                if role:
                    st.session_state.username = username
                    st.session_state.password = password
                    return "success"
                else:
                    st.write("LogIn Failed")
                    st.stop()
    else:
        return "success"

x = login_form(2)
if x == "success": 
    st.write(f"welcome {st.session_state.username}")
    st.write("You are on Home page")
    logout_button = st.button('logout')
    if logout_button:
        logout()
        st.stop()

#login stform authentication st-authenticator multipage discussion :speech_balloon: Show the Community! :cloud: Community Cloud

You should be clear about this.

There is technique on this.

This is not easy. You have to be familiar with the streamlit-authenticator.

Also stop calling people with @ (unless very important and emergency), I am only a moderator without compensation, I cannot provide 24/7 support. There are a lot of people here that can help.

I’m sorry… I will not do that. I just found your solution being very useful so tagged you to make sure that you see it atleast but its not compulory to answer. I’m sorry, again. Thank you for your efforts It had helped me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.