Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,123 @@
|
|
|
|
|
|
|
|
| 1 |
from simple_salesforce import Salesforce
|
| 2 |
-
|
| 3 |
# Salesforce Connection
|
| 4 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
result = sf.query(query)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"""
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bcrypt
|
| 2 |
+
import gradio as gr
|
| 3 |
from simple_salesforce import Salesforce
|
|
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
+
|
| 8 |
+
# Function to Hash Password
|
| 9 |
+
def hash_password(password):
|
| 10 |
+
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 11 |
+
|
| 12 |
+
# Function to Verify Password
|
| 13 |
+
def verify_password(plain_password, hashed_password):
|
| 14 |
+
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
| 15 |
+
|
| 16 |
+
# Signup function with email validation
|
| 17 |
+
def signup(name, email, phone, password):
|
| 18 |
try:
|
| 19 |
+
email = email.strip()
|
| 20 |
+
|
| 21 |
+
# Check if the email already exists in Salesforce
|
| 22 |
+
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
|
| 23 |
result = sf.query(query)
|
| 24 |
+
|
| 25 |
+
if len(result['records']) > 0:
|
| 26 |
+
# Email already exists
|
| 27 |
+
return "Email already exists! Please use a different email."
|
| 28 |
+
|
| 29 |
+
# Hash the password
|
| 30 |
+
hashed_password = hash_password(password)
|
| 31 |
+
|
| 32 |
+
# Create the new user record
|
| 33 |
+
sf.Customer_Login__c.create({
|
| 34 |
+
'Name': name.strip(),
|
| 35 |
+
'Email__c': email,
|
| 36 |
+
'Phone_Number__c': phone.strip(),
|
| 37 |
+
'Password__c': hashed_password # Use Password3__c field for hashed passwords
|
| 38 |
+
})
|
| 39 |
+
return "Signup successful! You can now login."
|
| 40 |
except Exception as e:
|
| 41 |
+
return f"Error during signup: {str(e)}"
|
| 42 |
+
|
| 43 |
+
# Login function
|
| 44 |
+
def login(email, password):
|
| 45 |
+
try:
|
| 46 |
+
email = email.strip()
|
| 47 |
+
password = password.strip()
|
| 48 |
+
|
| 49 |
+
# Query Salesforce for user details
|
| 50 |
+
query = f"SELECT Name, Email__c, Phone_Number__c, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
|
| 51 |
+
result = sf.query(query)
|
| 52 |
+
|
| 53 |
+
if len(result['records']) == 0:
|
| 54 |
+
return "Invalid email or password.", None, None, None
|
| 55 |
+
|
| 56 |
+
user = result['records'][0]
|
| 57 |
+
stored_password = user['Password__c']
|
| 58 |
+
|
| 59 |
+
print(f"Entered Email: {email}")
|
| 60 |
+
print(f"Entered Password: {password}")
|
| 61 |
+
print(f"Stored Hashed Password: {stored_password}")
|
| 62 |
+
|
| 63 |
+
# Verify the entered password with the stored hash
|
| 64 |
+
if verify_password(password, stored_password):
|
| 65 |
+
print("Password matched!")
|
| 66 |
+
return "Login successful!", user['Name'], user['Email__c'], user['Phone_Number__c']
|
| 67 |
else:
|
| 68 |
+
print("Password did not match!")
|
| 69 |
+
return "Invalid email or password.", None, None, None
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Error during login: {str(e)}")
|
| 72 |
+
return f"Error during login: {str(e)}", None, None, None
|
| 73 |
+
|
| 74 |
+
# Gradio Signup Page
|
| 75 |
+
def signup_page(name, email, phone, password):
|
| 76 |
+
response = signup(name, email, phone, password)
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
+
signup_interface = gr.Interface(
|
| 80 |
+
fn=signup_page,
|
| 81 |
+
inputs=[
|
| 82 |
+
gr.Textbox(label="Name"),
|
| 83 |
+
gr.Textbox(label="Email"),
|
| 84 |
+
gr.Textbox(label="Phone"),
|
| 85 |
+
gr.Textbox(label="Password", type="password"),
|
| 86 |
+
],
|
| 87 |
+
outputs=gr.Textbox(label="Signup Status"),
|
| 88 |
+
title="Signup Page"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Gradio Login Page
|
| 92 |
+
def login_page(email, password):
|
| 93 |
+
login_status, name, email, phone = login(email, password)
|
| 94 |
+
if login_status == "Login successful!":
|
| 95 |
+
return login_status, name, email, phone
|
| 96 |
+
else:
|
| 97 |
+
return login_status, "Error", "Error", "Error"
|
| 98 |
+
|
| 99 |
+
login_interface = gr.Interface(
|
| 100 |
+
fn=login_page,
|
| 101 |
+
inputs=[
|
| 102 |
+
gr.Textbox(label="Email"),
|
| 103 |
+
gr.Textbox(label="Password", type="password"),
|
| 104 |
+
],
|
| 105 |
+
outputs=[
|
| 106 |
+
gr.Textbox(label="Login Status"),
|
| 107 |
+
gr.Textbox(label="Name"),
|
| 108 |
+
gr.Textbox(label="Email"),
|
| 109 |
+
gr.Textbox(label="Phone"),
|
| 110 |
+
],
|
| 111 |
+
title="Login Page"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Combine Pages in Gradio
|
| 115 |
+
with gr.Blocks() as app:
|
| 116 |
+
with gr.Tab("Signup"):
|
| 117 |
+
signup_interface.render()
|
| 118 |
+
|
| 119 |
+
with gr.Tab("Login"):
|
| 120 |
+
login_interface.render()
|
| 121 |
+
|
| 122 |
+
# Launch Gradio App
|
| 123 |
+
app.launch()
|