File size: 3,952 Bytes
4ee4ce0
 
 
 
54f831b
 
bc1fd1a
f8d7ccb
 
 
54f831b
e033ca1
0279e41
 
4ee4ce0
 
 
0279e41
4ee4ce0
 
e033ca1
0279e41
54f831b
 
0279e41
 
 
 
 
 
 
 
 
 
 
 
 
 
54f831b
e033ca1
0279e41
e033ca1
0279e41
54f831b
 
 
 
 
4ee4ce0
54f831b
 
e033ca1
 
0ffcb00
e033ca1
4ee4ce0
54f831b
 
 
 
 
 
4ee4ce0
54f831b
d5abb5e
 
4ee4ce0
d5abb5e
4ee4ce0
 
d5abb5e
54f831b
 
d5abb5e
54f831b
 
d5abb5e
54f831b
 
bc1fd1a
54f831b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc1fd1a
54f831b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5abb5e
54f831b
 
 
 
 
 
 
d5abb5e
54f831b
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



import bcrypt
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', 
                password='Lavanyanaga@123', 
                security_token='z7Wvk6mys7n8XjqbYKf3bwBh7')



# Function to Hash Password
def hash_password(password):
    return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')

# Function to Verify Password
def verify_password(plain_password, hashed_password):
    return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))

# Signup function with email validation
def signup(name, email, phone, password):
    try:
        email = email.strip()

        # Check if the email already exists in Salesforce
        query = f"SELECT Id FROM User_Login__c WHERE Email__c = '{email}'"
        result = sf.query(query)

        if len(result['records']) > 0:
            # Email already exists
            return "Email already exists! Please use a different email."

        # Hash the password
        hashed_password = hash_password(password)

        # Create the new user record
        sf.User_Login__c.create({
            'Name__c': name.strip(),
            'Email__c': email,
            'Phone__c': phone.strip(),
            'Password3__c': hashed_password  # Use Password3__c field for hashed passwords
        })
        return "Signup successful! You can now login."
    except Exception as e:
        return f"Error during signup: {str(e)}"

# Login function
def login(email, password):
    try:
        email = email.strip()
        password = password.strip()

        # Query Salesforce for user details
        query = f"SELECT Name__c, Email__c, Phone__c, Password3__c FROM User_Login__c WHERE Email__c = '{email}'"
        result = sf.query(query)

        if len(result['records']) == 0:
            return "Invalid email or password.", None, None, None

        user = result['records'][0]
        stored_password = user['Password3__c']

        print(f"Entered Email: {email}")
        print(f"Entered Password: {password}")
        print(f"Stored Hashed Password: {stored_password}")

        # Verify the entered password with the stored hash
        if verify_password(password, stored_password):
            print("Password matched!")
            return "Login successful!", user['Name__c'], user['Email__c'], user['Phone__c']
        else:
            print("Password did not match!")
            return "Invalid email or password.", None, None, None
    except Exception as e:
        print(f"Error during login: {str(e)}")
        return f"Error during login: {str(e)}", None, None, None

# Gradio Signup Page
def signup_page(name, email, phone, password):
    response = signup(name, email, phone, password)
    return response

signup_interface = gr.Interface(
    fn=signup_page,
    inputs=[
        gr.Textbox(label="Name"),
        gr.Textbox(label="Email"),
        gr.Textbox(label="Phone"),
        gr.Textbox(label="Password", type="password"),
    ],
    outputs=gr.Textbox(label="Signup Status"),
    title="Signup Page"
)

# Gradio Login Page
def login_page(email, password):
    login_status, name, email, phone = login(email, password)
    if login_status == "Login successful!":
        return login_status, name, email, phone
    else:
        return login_status, "Error", "Error", "Error"

login_interface = gr.Interface(
    fn=login_page,
    inputs=[
        gr.Textbox(label="Email"),
        gr.Textbox(label="Password", type="password"),
    ],
    outputs=[
        gr.Textbox(label="Login Status"),
        gr.Textbox(label="Name"),
        gr.Textbox(label="Email"),
        gr.Textbox(label="Phone"),
    ],
    title="Login Page"
)

# Combine Pages in Gradio
with gr.Blocks() as app:
    with gr.Tab("Signup"):
        signup_interface.render()

    with gr.Tab("Login"):
        login_interface.render()

# Launch Gradio App
app.launch()