nagasurendra commited on
Commit
1e29ad7
·
verified ·
1 Parent(s): aae5506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -50
app.py CHANGED
@@ -1,59 +1,94 @@
1
- import gradio as gr
2
- import requests
3
-
4
- # Salesforce API endpoint and authentication
5
- SALESFORCE_API_URL = "https://your-instance.salesforce.com/services/apexrest/UserHandler/"
6
- AUTH_TOKEN = "Your_Auth_Token"
7
-
8
- # Helper function for Salesforce API calls
9
- def call_salesforce_api(endpoint, payload):
10
- headers = {
11
- "Authorization": f"Bearer {AUTH_TOKEN}",
12
- "Content-Type": "application/json",
13
- }
14
- response = requests.post(SALESFORCE_API_URL + endpoint, json=payload, headers=headers)
15
- return response.json()
16
 
17
- # Signup function
18
- def signup(name, phone, email, password):
19
- payload = {"name": name, "email": email, "phone": phone, "password": password}
20
- result = call_salesforce_api("registerUser", payload)
21
- if result == "Signup successful":
22
- return gr.update(visible=False), gr.update(visible=True), "Signup successful! Please login."
23
- return None, None, result
24
 
25
- # Login function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def login(email, password):
27
- payload = {"email": email, "password": password}
28
- valid = call_salesforce_api("validateLogin", payload)
29
- if valid:
30
- return gr.update(visible=False), gr.update(visible=True), "Login successful!"
31
- return None, None, "Invalid credentials. Please try again."
32
 
33
- # Gradio app components
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  with gr.Blocks() as app:
35
- with gr.Row(visible=True) as login_page:
36
- gr.Textbox(label="Email", placeholder="Enter your email", key="login_email")
37
- gr.Textbox(label="Password", placeholder="Enter your password", type="password", key="login_password")
38
- gr.Button("Login", key="login_button").click(
39
- login, inputs=["login_email", "login_password"], outputs=[login_page, "home_page", "message"]
40
- )
41
- gr.Button("Signup").click(None, [], [login_page, "signup_page"])
42
-
43
- with gr.Row(visible=False) as signup_page:
44
- gr.Textbox(label="Name", placeholder="Enter your name", key="signup_name")
45
- gr.Textbox(label="Phone", placeholder="Enter your phone number", key="signup_phone")
46
- gr.Textbox(label="Email", placeholder="Enter your email", key="signup_email")
47
- gr.Textbox(label="Password", placeholder="Enter your password", type="password", key="signup_password")
48
- gr.Button("Sign Up", key="signup_button").click(
49
- signup, inputs=["signup_name", "signup_phone", "signup_email", "signup_password"], outputs=[signup_page, "login_page", "message"]
50
- )
51
- gr.Button("Back to Login").click(None, [], [signup_page, "login_page"])
52
 
53
- with gr.Row(visible=False) as home_page:
54
- gr.Label(value="Welcome to the Home Page!")
55
- gr.Button("Logout").click(None, [], [home_page, "login_page"])
56
 
57
- gr.Label(value="", key="message")
 
58
 
 
59
  app.launch()
 
1
+ from simple_salesforce import Salesforce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ # Authenticate with Salesforce
4
+ sf = Salesforce(username='[email protected]',
5
+ password='Lavanyanaga@123',
6
+ security_token='z7Wvk6mys7n8XjqbYKf3bwBh7')
7
+ import bcrypt
 
 
8
 
9
+ def hash_password(password):
10
+ return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
11
+ def verify_password(plain_password, hashed_password):
12
+ return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
13
+ def signup(name, email, phone, password):
14
+ hashed_password = hash_password(password)
15
+ try:
16
+ sf.User_Login__c.create({
17
+ 'Name__c': name,
18
+ 'Email__c': email,
19
+ 'Phone__c': phone,
20
+ 'Password__c': hashed_password
21
+ })
22
+ return "Signup successful! You can now login."
23
+ except Exception as e:
24
+ return f"Error during signup: {str(e)}"
25
  def login(email, password):
26
+ query = f"SELECT Id, Name__c, Email__c, Password__c FROM User_Login__c WHERE Email__c = '{email}'"
27
+ result = sf.query(query)
28
+
29
+ if len(result['records']) == 0:
30
+ return "Invalid email or password."
31
 
32
+ user = result['records'][0]
33
+ if verify_password(password, user['Password__c']):
34
+ return f"Welcome, {user['Name__c']}!", user
35
+ else:
36
+ return "Invalid email or password.", None
37
+ import gradio as gr
38
+ def signup_page(name, email, phone, password):
39
+ response = signup(name, email, phone, password)
40
+ return response
41
+ def login_page(email, password):
42
+ response, user = login(email, password)
43
+ if user:
44
+ return response, user['Name__c'], user['Email__c'], user['Phone__c']
45
+ return response, None, None, None
46
+ def home_page(name, email, phone):
47
+ return f"Welcome, {name}! Your email: {email}, Phone: {phone}"
48
+ signup_interface = gr.Interface(
49
+ fn=signup_page,
50
+ inputs=[
51
+ gr.Textbox(label="Name"),
52
+ gr.Textbox(label="Email"),
53
+ gr.Textbox(label="Phone"),
54
+ gr.Textbox(label="Password", type="password"),
55
+ ],
56
+ outputs=gr.Textbox(label="Signup Status"),
57
+ title="Signup Page"
58
+ )
59
+ login_interface = gr.Interface(
60
+ fn=login_page,
61
+ inputs=[
62
+ gr.Textbox(label="Email"),
63
+ gr.Textbox(label="Password", type="password"),
64
+ ],
65
+ outputs=[
66
+ gr.Textbox(label="Login Status"),
67
+ gr.Textbox(label="Name"),
68
+ gr.Textbox(label="Email"),
69
+ gr.Textbox(label="Phone"),
70
+ ],
71
+ title="Login Page"
72
+ )
73
+ home_interface = gr.Interface(
74
+ fn=home_page,
75
+ inputs=[
76
+ gr.Textbox(label="Name"),
77
+ gr.Textbox(label="Email"),
78
+ gr.Textbox(label="Phone"),
79
+ ],
80
+ outputs=gr.Textbox(label="Welcome Message"),
81
+ title="Home Page"
82
+ )
83
  with gr.Blocks() as app:
84
+ with gr.Tab("Signup"):
85
+ signup_interface.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ with gr.Tab("Login"):
88
+ login_interface.render()
 
89
 
90
+ with gr.Tab("Home"):
91
+ home_interface.render()
92
 
93
+ # Launch the app
94
  app.launch()