import bcrypt import gradio as gr from simple_salesforce import Salesforce # Authenticate with Salesforce sf = Salesforce(username='surendra@sathkrutha.com', password='Lavanyanaga@123', security_token='z7Wvk6mys7n8XjqbYKf3bwBh7') # Function to Hash Password def hash_password(password): return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') # Function to Verify Password def verify_password(plain_password, hashed_password): return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) # Function for Signup def signup(name, email, phone, password): hashed_password = hash_password(password) # Hash the plain text password try: sf.User_Login__c.create({ 'Name__c': name, 'Email__c': email, 'Phone__c': phone, 'Password__c': hashed_password }) return "Signup successful! You can now login." except Exception as e: return f"Error during signup: {str(e)}" # Function for Login def login(email, password): try: # Query Salesforce for the user's hashed password query = f"SELECT Name__c, Email__c, Phone__c, Password__c FROM User_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if len(result['records']) == 0: return "Invalid email or password.", None, None, None user = result['records'][0] hashed_password = user['Password__c'] # Compare the entered password with the stored hash if verify_password(password, hashed_password): return "Login successful!", user['Name__c'], user['Email__c'], user['Phone__c'] else: return "Invalid email or password.", None, None, None except Exception as e: return f"Error during login: {str(e)}", None, None, None # Function for Home Page def home_page(name, email, phone): return f"Welcome, {name}! Your email is {email} and your phone number is {phone}." # Gradio Signup Interface def signup_page(name, email, phone, password): response = signup(name, email, phone, password) return response signup_interface = gr.Interface( fn=signup_page, inputs=[ gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), gr.Textbox(label="Password", type="password"), ], outputs=gr.Textbox(label="Signup Status"), title="Signup Page" ) # Gradio Login Interface def login_page(email, password): login_status, name, email, phone = login(email, password) if login_status == "Login successful!": return login_status, name, email, phone else: return login_status, "Error", "Error", "Error" login_interface = gr.Interface( fn=login_page, inputs=[ gr.Textbox(label="Email"), gr.Textbox(label="Password", type="password"), ], outputs=[ gr.Textbox(label="Login Status"), gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), ], title="Login Page" ) # Gradio Home Interface home_interface = gr.Interface( fn=home_page, inputs=[ gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), ], outputs=gr.Textbox(label="Welcome Message"), title="Home Page" ) # Combine All Pages in Gradio with gr.Blocks() as app: with gr.Tab("Signup"): signup_interface.render() with gr.Tab("Login"): login_interface.render() with gr.Tab("Home"): home_interface.render() # Launch the Gradio App app.launch() """def hash_password(password): # This will generate a hash that may exceed 60 characters return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') def verify_password(plain_password, hashed_password): return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) def signup(name, email, phone, password): hashed_password = hash_password(password) try: sf.User_Login__c.create({ 'Name__c': name, 'Email__c': email, 'Phone__c': phone, 'Password__c': hashed_password }) return "Signup successful! You can now login." except Exception as e: return f"Error during signup: {str(e)}" def login(email, password): # Query Salesforce for the user's hashed password query = f"SELECT Name__c, Email__c, Phone__c, Password__c FROM User_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if len(result['records']) == 0: return "Invalid email or password.", None, None, None user = result['records'][0] hashed_password = user['Password__c'] # Compare the entered password with the stored hash if bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8')): return "Login successful!", user['Name__c'], user['Email__c'], user['Phone__c'] else: return "Invalid email or password.", None, None, None import gradio as gr def signup_page(name, email, phone, password): response = signup(name, email, phone, password) return response def login_page(email, password): response, user = login(email, password) if user: return response, user['Name__c'], user['Email__c'], user['Phone__c'] return response, None, None, None def home_page(name, email, phone): return f"Welcome, {name}! Your email: {email}, Phone: {phone}" signup_interface = gr.Interface( fn=signup_page, inputs=[ gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), gr.Textbox(label="Password", type="password"), ], outputs=gr.Textbox(label="Signup Status"), title="Signup Page" ) login_interface = gr.Interface( fn=login_page, inputs=[ gr.Textbox(label="Email"), gr.Textbox(label="Password", type="password"), ], outputs=[ gr.Textbox(label="Login Status"), gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), ], title="Login Page" ) home_interface = gr.Interface( fn=home_page, inputs=[ gr.Textbox(label="Name"), gr.Textbox(label="Email"), gr.Textbox(label="Phone"), ], outputs=gr.Textbox(label="Welcome Message"), title="Home Page" ) with gr.Blocks() as app: with gr.Tab("Signup"): signup_interface.render() with gr.Tab("Login"): login_interface.render() with gr.Tab("Home"): home_interface.render() # Launch the app app.launch()"""