nagasurendra commited on
Commit
3d1ceed
·
verified ·
1 Parent(s): bad40cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -4
app.py CHANGED
@@ -1,10 +1,89 @@
1
  import gradio as gr
2
  import pandas as pd
3
  from bcrypt import hashpw, gensalt, checkpw
4
- from utils.database_handler import save_user, check_credentials
5
 
6
- # File for storing user data
7
- USER_FILE = "users.xlsx"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  # Function to load the menu data
@@ -254,7 +333,6 @@ button {
254
  cartPage.style.display = 'none';
255
  finalOrderPage.style.display = 'block';
256
  }
257
-
258
  </script>
259
  """
260
  # Authentication and Navigation Logic
 
1
  import gradio as gr
2
  import pandas as pd
3
  from bcrypt import hashpw, gensalt, checkpw
 
4
 
5
+ import bcrypt
6
+ import gradio as gr
7
+ from simple_salesforce import Salesforce
8
+
9
+ # Salesforce Connection
10
+ # Salesforce Connection
11
+ sf = Salesforce(username='surendra@sathkrutha.com',
12
+ password='Lavanyanaga@123',
13
+ security_token='z7Wvk6mys7n8XjqbYKf3bwBh7')
14
+ # Utility Functions for Salesforce
15
+
16
+ def save_user(name, phone, email, password):
17
+ """Save user details to Salesforce."""
18
+ try:
19
+ # Check if email already exists in Salesforce
20
+ query = f"SELECT Id FROM User_Login__c WHERE Email__c = '{email}'"
21
+ result = sf.query(query)
22
+
23
+ if len(result['records']) > 0:
24
+ return False, "Email already exists. Please use a different email."
25
+
26
+ # Hash the password
27
+ hashed_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
28
+
29
+ # Create the new user record in Salesforce
30
+ sf.User_Login__c.create({
31
+ 'Name__c': name.strip(),
32
+ 'Phone__c': phone.strip(),
33
+ 'Email__c': email.strip(),
34
+ 'Password3__c': hashed_password # Use Password3__c to store hashed passwords
35
+ })
36
+ return True, "Signup successful! You can now login."
37
+ except Exception as e:
38
+ print(f"Error saving user to Salesforce: {e}")
39
+ return False, "An error occurred during signup. Please try again."
40
+
41
+ def check_credentials(email, password):
42
+ """Check user credentials during login."""
43
+ try:
44
+ # Query Salesforce for user details
45
+ query = f"SELECT Password3__c FROM User_Login__c WHERE Email__c = '{email}'"
46
+ result = sf.query(query)
47
+
48
+ if len(result['records']) == 0:
49
+ return False, "Invalid email or password."
50
+
51
+ # Retrieve the stored hashed password
52
+ stored_password = result['records'][0]['Password3__c']
53
+
54
+ # Verify the entered password with the stored hashed password
55
+ if bcrypt.checkpw(password.encode(), stored_password.encode()):
56
+ return True, "Login successful!"
57
+ else:
58
+ return False, "Invalid email or password."
59
+ except Exception as e:
60
+ print(f"Error checking credentials: {e}")
61
+ return False, "An error occurred during login. Please try again."
62
+
63
+ # Authentication and Navigation Logic
64
+ def authenticate_user(email, password):
65
+ """Authenticate user during login."""
66
+ success, message = check_credentials(email, password)
67
+ if success:
68
+ return gr.update(visible=False), gr.update(visible=True), message
69
+ else:
70
+ return gr.update(visible=True), gr.update(visible=False), message
71
+
72
+ def navigate_to_signup():
73
+ """Navigate to the signup page."""
74
+ return gr.update(visible=False), gr.update(visible=True)
75
+
76
+ def create_account(name, phone, email, password):
77
+ """Create a new account during signup."""
78
+ success, message = save_user(name, phone, email, password)
79
+ if success:
80
+ return message, gr.update(visible=True), gr.update(visible=False)
81
+ else:
82
+ return message, gr.update(visible=False), gr.update(visible=True)
83
+
84
+ def navigate_to_login():
85
+ """Navigate back to the login page."""
86
+ return gr.update(visible=True), gr.update(visible=False)
87
 
88
 
89
  # Function to load the menu data
 
333
  cartPage.style.display = 'none';
334
  finalOrderPage.style.display = 'block';
335
  }
 
336
  </script>
337
  """
338
  # Authentication and Navigation Logic