Sign in page

Summary

cant login to the plot page after sign up.
Error: log in failed

Steps to reproduce

My goal is to have a landing page where user can signup and then login with their user-ID and password and then take them to the plot app page
Code snippet:

import streamlit as st
import sqlite3
import bcrypt
from sample import plot_page  # Import the plot page

# Database setup
conn = sqlite3.connect('user.db')
cursor = conn.cursor()

def create_table():
    cursor.execute('''CREATE TABLE IF NOT EXISTS users
                  (uid TEXT PRIMARY KEY, email TEXT, password TEXT)''')
    conn.commit()

def insert_user(email, password, uid):
    hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
    cursor.execute("INSERT INTO users (uid, email, password) VALUES (?, ?, ?)", (uid, email, hashed_password))
    conn.commit()
    

def get_user_by_email(email):
    cursor.execute("SELECT * FROM users WHERE email=?", (email))
    data=cursor.fetchall()
    return data

def verify_password(password, hashed_password):
    return bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8'))

# App functions
def app():
    create_table()
    st.title('Welcome to :blue[Taiwo]')

    if 'username' not in st.session_state:
        st.session_state.username = ''
    if 'useremail' not in st.session_state:
        st.session_state.useremail = ''
    if 'loggedin' not in st.session_state:
        st.session_state.loggedin = False  # New state variable to track login status

    def f():
        try:
            user = get_user_by_email(email)
            if user and verify_password(password, user[2]):
                st.session_state.username = user[0]
                st.session_state.useremail = user[1]
                st.session_state.loggedin = True  # Set login status to True
            else:
                st.warning('Login Failed')
        except:
            st.warning('Login Failed')

    def t():
        st.session_state.loggedin = False  # Set login status to False
        st.session_state.username = ''
        st.session_state.useremail = ''

    if not st.session_state.loggedin:
        choice = st.selectbox('Login/Signup', ['Login', 'Sign up'])
        email = st.text_input('Email Address')
        password = st.text_input('Password', type='password')

        if choice == 'Sign up':
            username = st.text_input("Enter your unique username")

            if st.button('Create my account'):
                insert_user(email, password, username)
                st.success('Account created successfully!')
                st.markdown('Please Login using your email and password')
                st.balloons()
        else:
            st.button('Login', on_click=f)

    if st.session_state.loggedin:
        st.text('Username: ' + st.session_state.username)
        st.text('Email id: ' + st.session_state.useremail)
        st.button('Sign out', on_click=t)
        plot_page()  # Display plot page if logged in

# Run the app
if __name__ == '__main__':
    app()

@snehankekre please can you help with this.

Thanks

@Taiwo_Osunrinde Please check your log in details, if there has any mistakes then fix them.

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