Spaces:
Runtime error
Runtime error
| import random | |
| from datetime import datetime, timedelta | |
| from salesforce_connection import connect_to_salesforce | |
| # Generate a 6-digit OTP | |
| def generate_otp(): | |
| return str(random.randint(100000, 999999)) | |
| # Login logic | |
| def login_user(email, password): | |
| sf = connect_to_salesforce() | |
| query = f"SELECT Id, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" | |
| result = sf.query(query) | |
| if result['totalSize'] == 0: | |
| return {"status": "error", "message": "Invalid email"} | |
| user = result['records'][0] | |
| if user['Password__c'] == password: | |
| sf.Customer_Login__c.update(user['Id'], {"Login_Status__c": "Logged In"}) | |
| return {"status": "success", "message": "Login successful"} | |
| else: | |
| return {"status": "error", "message": "Invalid password"} | |
| # Signup logic | |
| def signup_user(name, email, phone, password): | |
| sf = connect_to_salesforce() | |
| query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" | |
| result = sf.query(query) | |
| if result['totalSize'] > 0: | |
| return {"status": "error", "message": "Email already exists"} | |
| otp = generate_otp() | |
| expiration_time = (datetime.now() + timedelta(minutes=2)).strftime('%Y-%m-%dT%H:%M:%S') | |
| sf.Customer_Login__c.create({ | |
| "Name": name, | |
| "Email__c": email, | |
| "Phone_Number__c": phone, | |
| "Password__c": password, | |
| "OTP__c": otp, | |
| "OTP_Expiration__c": expiration_time, | |
| "Login_Status__c": "Logged Out" | |
| }) | |
| print(f"OTP for {email}: {otp}") # Replace this with email-sending logic | |
| return {"status": "success", "message": "Signup successful. OTP sent to email."} | |
| # OTP validation logic | |
| def validate_otp(email, otp): | |
| sf = connect_to_salesforce() | |
| query = f"SELECT Id, OTP__c, OTP_Expiration__c FROM Customer_Login__c WHERE Email__c = '{email}'" | |
| result = sf.query(query) | |
| if result['totalSize'] == 0: | |
| return {"status": "error", "message": "Invalid email"} | |
| user = result['records'][0] | |
| if user['OTP__c'] != otp: | |
| return {"status": "error", "message": "Invalid OTP"} | |
| expiration_time = datetime.strptime(user['OTP_Expiration__c'], '%Y-%m-%dT%H:%M:%S.%fZ') | |
| if datetime.now() > expiration_time: | |
| return {"status": "error", "message": "OTP expired"} | |
| return {"status": "success", "message": "OTP validated"} | |
| # Forgot password logic | |
| def forgot_password(email, new_password, otp): | |
| sf = connect_to_salesforce() | |
| otp_validation = validate_otp(email, otp) | |
| if otp_validation['status'] == "error": | |
| return otp_validation | |
| query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" | |
| result = sf.query(query) | |
| if result['totalSize'] == 0: | |
| return {"status": "error", "message": "Invalid email"} | |
| user_id = result['records'][0]['Id'] | |
| sf.Customer_Login__c.update(user_id, {"Password__c": new_password}) | |
| return {"status": "success", "message": "Password reset successful"} | |