Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fields parameter of the register_user doesn't work #245

Open
shiwei2007 opened this issue Dec 12, 2024 · 3 comments
Open

fields parameter of the register_user doesn't work #245

shiwei2007 opened this issue Dec 12, 2024 · 3 comments

Comments

@shiwei2007
Copy link

I want a simple register form with only email and password. So I specified the fields as the following,
register_user(fields={'Form name':'Register',
'Username':'Email',
'Password':'Password',
'Repeat password':'Repeat password'}
Unfortunately the page does not change accordingly.

@kevintcaron
Copy link

kevintcaron commented Dec 13, 2024

The README file says the fields parameter "Customizes the text of headers, buttons and other fields". It can be used to change the text in those fields to, for example, support a login in a different language like Spanish. I do not believe fields can be currently used to specify which fields are required in Authenticate.register_user. @mkhorasani has confirmed in issue 234 that a future release will support the ability to disable "Password hint".

At the moment, it appears all of the existing Register User fields are required except capcha which can be disabled with Authenticate.register_user(capcha=False). @mkhorasani, please correct me if I got any of this wrong.

@JurijsNazarovs
Copy link

That seems to be correct. I checked the code for register_user, and all those fields, like First Name are hardcoded as buttons in streamlitapp. Also, all other functions (going upper in class) register_user require first name and other field to be non optional.

I currently have the same issue, that i want a very simple form, email and password, and wondering what would be the best solution. Just update credentials manually and save it back to yml file? In that case, would I still have all checks available, like email check and etc.

@JurijsNazarovs
Copy link

JurijsNazarovs commented Dec 29, 2024

@shiwei2007 you can use the following code:

import streamlit as st
import streamlit_authenticator as stauth

# File to store user credentials and configuration
USER_CONFIG_FILE = "./user_credentials.yaml"


# Registration Page
def register_page():
    st.title("Register")

    # Initialize the authenticator
    authenticator = stauth.Authenticate(credentials=USER_CONFIG_FILE)

    # Input fields for email, password, and optional username
    email = st.text_input("Email")
    username = st.text_input("Username (optional or email will be used)")
    password = st.text_input("Password", type="password")

    if st.button("Register"):
        # Validation
        if not email or not password:
            st.error("Email and password are required.")
            return

        # Use email as username if username is not provided
        username = username.strip() if username.strip() else email

        try:
            # Register the user
            authenticator.authentication_controller.authentication_model.register_user(
                new_first_name="",
                new_last_name="",
                new_email=email,
                new_username=username,  # Use provided or fallback to email
                new_password=password,
                password_hint="",  # No password hint
                pre_authorized=None,  # No preauthorization check
            )

            st.success("User registered successfully!")

        except Exception as e:
            st.error(f"An error occurred during registration: {e}")


# Login Page
def login_page():
    st.title("Login")

    # Initialize the authenticator
    authenticator = stauth.Authenticate(credentials=USER_CONFIG_FILE)

    # Render the login widget
    authenticator.login("main")  # No longer returns values directly

    # Retrieve authentication status and username from session state
    authentication_status = st.session_state.get("authentication_status")
    username = st.session_state.get("username")

    if authentication_status:
        st.success(f"Welcome, {username}!")
        st.session_state["logged_in"] = True
        st.rerun()
    elif authentication_status is False:
        st.error("Invalid username or password.")
    elif authentication_status is None:
        st.warning("Please enter your username and password.")


def logout_button(authenticator):
    """
    Logs the user out by resetting the session state and using the authenticator's logout method.
    """

    def handle_logout(*args, **kwargs):
        # Custom logic after logout
        st.session_state["logged_in"] = False
        st.session_state["username"] = None
        st.rerun()  # Redirect to the main registration menu

    # Logout button with callback
    authenticator.logout(
        "Logout", "main", key="logout_authenticator", callback=handle_logout
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@JurijsNazarovs @kevintcaron @shiwei2007 and others