Spaces:
Runtime error
Runtime error
File size: 5,870 Bytes
8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d 62b6ece 8e40f0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | # app.py
import gradio as gr
from simple_salesforce import Salesforce
from datetime import datetime, timedelta
import random
# Salesforce Authentication
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
otp_data = {} # Temporary OTP storage
# Navigation state
current_page = "login"
def set_page(page):
global current_page
current_page = page
# Login Page Function
def login(email, password):
global current_page
try:
query_result = sf.query(f"SELECT Password__c FROM Customer_Login__c WHERE Email__c = '{email}'")
if not query_result['records']:
return "Error: Email not found."
stored_password = query_result['records'][0]['Password__c']
if password != stored_password:
return "Error: Invalid password."
sf.Customer_Login__c.update(query_result['records'][0]['Id'], {'Login_Status__c': 'Logged In'})
set_page("menu")
return "Login successful! Redirecting to menu..."
except Exception as e:
return f"Error: {str(e)}"
def login_ui():
return gr.Interface(
fn=login,
inputs=[gr.Textbox(label="Email"), gr.Password(label="Password")],
outputs="text",
title="Login Page"
)
# Signup Page Functions
def send_otp(email):
try:
query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
if query_result['records']:
return "Error: Email already registered."
otp = random.randint(100000, 999999)
otp_data[email] = {
"otp": otp,
"expiration": datetime.now() + timedelta(minutes=2)
}
print(f"OTP for {email}: {otp}") # Simulated email sending
return "OTP sent successfully! Check your email."
except Exception as e:
return f"Error: {str(e)}"
def verify_otp(email, otp, name, password, phone):
try:
if email not in otp_data or otp_data[email]['expiration'] < datetime.now():
return "Error: OTP expired or invalid."
if otp_data[email]['otp'] != int(otp):
return "Error: Incorrect OTP."
if not (6 <= len(password) <= 10 and
any(c.isupper() for c in password) and
any(c.islower() for c in password) and
any(c.isdigit() for c in password) and
any(c in "!@#$%^&*" for c in password)):
return "Error: Password does not meet complexity requirements."
sf.Customer_Login__c.create({
"Name": name,
"Email__c": email,
"Password__c": password,
"Phone_Number__c": phone,
"Login_Status__c": "Logged Out"
})
del otp_data[email]
set_page("login")
return "Signup successful! Redirecting to login page..."
except Exception as e:
return f"Error: {str(e)}"
def signup_ui():
return gr.Interface(
fn=lambda email, otp, name, password, phone: (
send_otp(email) if otp == "" else verify_otp(email, otp, name, password, phone)
),
inputs=[
gr.Textbox(label="Email"),
gr.Textbox(label="OTP (Leave blank to send OTP)"),
gr.Textbox(label="Name"),
gr.Password(label="Password"),
gr.Textbox(label="Phone Number")
],
outputs="text",
title="Signup Page"
)
# Forgot Password Page Function
def forgot_password(email, otp, new_password):
global current_page
try:
query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
if not query_result['records']:
return "Error: Email not found."
if otp == "":
otp = random.randint(100000, 999999)
otp_data[email] = {
"otp": otp,
"expiration": datetime.now() + timedelta(minutes=2)
}
print(f"OTP for {email}: {otp}")
return "OTP sent successfully! Check your email."
if email in otp_data and otp_data[email]['otp'] == int(otp) and otp_data[email]['expiration'] > datetime.now():
if not (6 <= len(new_password) <= 10 and
any(c.isupper() for c in new_password) and
any(c.islower() for c in new_password) and
any(c.isdigit() for c in new_password) and
any(c in "!@#$%^&*" for c in new_password)):
return "Error: Password does not meet complexity requirements."
sf.Customer_Login__c.update(query_result['records'][0]['Id'], {"Password__c": new_password})
del otp_data[email]
set_page("login")
return "Password updated successfully! Redirecting to login page..."
return "Error: Invalid or expired OTP."
except Exception as e:
return f"Error: {str(e)}"
def forgot_password_ui():
return gr.Interface(
fn=forgot_password,
inputs=[
gr.Textbox(label="Email"),
gr.Textbox(label="OTP (Leave blank to send OTP)"),
gr.Password(label="New Password")
],
outputs="text",
title="Forgot Password Page"
)
# Menu Page Function
def menu_page():
global current_page
return "Welcome to the Menu Page! Select your options here."
def menu_ui():
return gr.Interface(
fn=menu_page,
inputs=None,
outputs="text",
title="Menu Page"
)
# Main App Logic
def render_page():
if current_page == "login":
return login_ui()
elif current_page == "signup":
return signup_ui()
elif current_page == "forgot_password":
return forgot_password_ui()
elif current_page == "menu":
return menu_ui()
# Launch App
app = render_page()
app.launch()
|