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|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from simplesalesforce import Salesforce
|
| 3 |
+
import re
|
| 4 |
+
import random
|
| 5 |
+
import smtplib
|
| 6 |
+
from email.mime.text import MIMEText
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
|
| 9 |
+
# Salesforce Connection
|
| 10 |
+
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 11 |
+
|
| 12 |
+
# Helper Functions
|
| 13 |
+
otp_storage = {}
|
| 14 |
+
|
| 15 |
+
def send_otp(email):
|
| 16 |
+
otp = random.randint(100000, 999999)
|
| 17 |
+
otp_expiration = datetime.now() + timedelta(minutes=2)
|
| 18 |
+
otp_storage[email] = {'otp': otp, 'expires': otp_expiration}
|
| 19 |
+
|
| 20 |
+
# Sending email (Replace with actual SMTP setup)
|
| 21 |
+
msg = MIMEText(f"Your OTP is {otp}. It expires in 2 minutes.")
|
| 22 |
+
msg['Subject'] = 'Your OTP'
|
| 23 |
+
msg['From'] = 'your_email@example.com'
|
| 24 |
+
msg['To'] = email
|
| 25 |
+
try:
|
| 26 |
+
with smtplib.SMTP('smtp.example.com', 587) as server:
|
| 27 |
+
server.starttls()
|
| 28 |
+
server.login('your_email@example.com', 'your_email_password')
|
| 29 |
+
server.send_message(msg)
|
| 30 |
+
return "OTP sent successfully."
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error sending OTP: {str(e)}"
|
| 33 |
+
|
| 34 |
+
def validate_email(email):
|
| 35 |
+
regex = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
| 36 |
+
return re.match(regex, email)
|
| 37 |
+
|
| 38 |
+
def validate_password(password):
|
| 39 |
+
if (len(password) >= 6 and len(password) <= 10 and
|
| 40 |
+
re.search(r'[A-Z]', password) and
|
| 41 |
+
re.search(r'[a-z]', password) and
|
| 42 |
+
re.search(r'\d', password) and
|
| 43 |
+
re.search(r'[!@#$%^&*]', password)):
|
| 44 |
+
return True
|
| 45 |
+
return False
|
| 46 |
+
|
| 47 |
+
# Functions for Gradio
|
| 48 |
+
|
| 49 |
+
def login(email, password):
|
| 50 |
+
if not email or not password:
|
| 51 |
+
return "Email and Password cannot be empty."
|
| 52 |
+
|
| 53 |
+
query = sf.query(f"SELECT Id, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'")
|
| 54 |
+
if not query['records']:
|
| 55 |
+
return "Invalid email or password."
|
| 56 |
+
|
| 57 |
+
record = query['records'][0]
|
| 58 |
+
if password == record['Password__c']:
|
| 59 |
+
sf.Customer_Login__c.update(record['Id'], {'Login_Status__c': 'Logged In'})
|
| 60 |
+
return "Login successful. Redirecting to the menu page..."
|
| 61 |
+
else:
|
| 62 |
+
return "Invalid email or password."
|
| 63 |
+
|
| 64 |
+
def signup(name, email, password, phone, otp, verify_otp):
|
| 65 |
+
if not all([name, email, password, phone, otp]):
|
| 66 |
+
return "All fields are required."
|
| 67 |
+
|
| 68 |
+
if not validate_email(email):
|
| 69 |
+
return "Invalid email format."
|
| 70 |
+
|
| 71 |
+
if not validate_password(password):
|
| 72 |
+
return "Password does not meet complexity requirements."
|
| 73 |
+
|
| 74 |
+
if otp_storage.get(email) and otp_storage[email]['otp'] == int(otp):
|
| 75 |
+
if datetime.now() > otp_storage[email]['expires']:
|
| 76 |
+
return "OTP has expired. Please resend OTP."
|
| 77 |
+
|
| 78 |
+
# Store data in Salesforce
|
| 79 |
+
try:
|
| 80 |
+
sf.Customer_Login__c.create({
|
| 81 |
+
'Name': name,
|
| 82 |
+
'Email__c': email,
|
| 83 |
+
'Password__c': password,
|
| 84 |
+
'Phone_Number__c': phone,
|
| 85 |
+
'OTP__c': str(otp),
|
| 86 |
+
'OTP_Expiration__c': otp_storage[email]['expires'],
|
| 87 |
+
'Login_Status__c': 'Logged Out'
|
| 88 |
+
})
|
| 89 |
+
return "Signup successful. Please log in."
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return f"Error storing data: {str(e)}"
|
| 92 |
+
else:
|
| 93 |
+
return "Invalid OTP."
|
| 94 |
+
|
| 95 |
+
def forgot_password(email, otp, new_password, confirm_password):
|
| 96 |
+
if not email:
|
| 97 |
+
return "Email is required."
|
| 98 |
+
|
| 99 |
+
if not validate_email(email):
|
| 100 |
+
return "Invalid email format."
|
| 101 |
+
|
| 102 |
+
if email in otp_storage and otp_storage[email]['otp'] == int(otp):
|
| 103 |
+
if datetime.now() > otp_storage[email]['expires']:
|
| 104 |
+
return "OTP has expired."
|
| 105 |
+
|
| 106 |
+
if new_password != confirm_password:
|
| 107 |
+
return "Passwords do not match."
|
| 108 |
+
|
| 109 |
+
if not validate_password(new_password):
|
| 110 |
+
return "Password does not meet complexity requirements."
|
| 111 |
+
|
| 112 |
+
# Update password in Salesforce
|
| 113 |
+
query = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
|
| 114 |
+
if query['records']:
|
| 115 |
+
record_id = query['records'][0]['Id']
|
| 116 |
+
sf.Customer_Login__c.update(record_id, {'Password__c': new_password})
|
| 117 |
+
return "Password updated successfully."
|
| 118 |
+
else:
|
| 119 |
+
return "Email not found."
|
| 120 |
+
else:
|
| 121 |
+
return "Invalid OTP."
|
| 122 |
+
|
| 123 |
+
# Gradio Interface
|
| 124 |
+
login_ui = gr.Interface(
|
| 125 |
+
fn=login,
|
| 126 |
+
inputs=[gr.Textbox(label="Email"), gr.Textbox(label="Password", type="password")],
|
| 127 |
+
outputs="text",
|
| 128 |
+
title="Login Page"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
signup_ui = gr.Interface(
|
| 132 |
+
fn=signup,
|
| 133 |
+
inputs=[
|
| 134 |
+
gr.Textbox(label="Name"),
|
| 135 |
+
gr.Textbox(label="Email"),
|
| 136 |
+
gr.Textbox(label="Password", type="password"),
|
| 137 |
+
gr.Textbox(label="Phone Number"),
|
| 138 |
+
gr.Textbox(label="Enter OTP"),
|
| 139 |
+
],
|
| 140 |
+
outputs="text",
|
| 141 |
+
title="Signup Page"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
forgot_password_ui = gr.Interface(
|
| 145 |
+
fn=forgot_password,
|
| 146 |
+
inputs=[
|
| 147 |
+
gr.Textbox(label="Email"),
|
| 148 |
+
gr.Textbox(label="OTP"),
|
| 149 |
+
gr.Textbox(label="New Password", type="password"),
|
| 150 |
+
gr.Textbox(label="Confirm Password", type="password")
|
| 151 |
+
],
|
| 152 |
+
outputs="text",
|
| 153 |
+
title="Forgot Password"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
dashboard = gr.TabbedInterface(
|
| 157 |
+
[login_ui, signup_ui, forgot_password_ui],
|
| 158 |
+
tab_names=["Login", "Signup", "Forgot Password"]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
dashboard.launch()
|