from flask import Flask, request, jsonify
from flask_cors import CORS
import requests
import random
import time
import os
app = Flask(__name__)
CORS(app)
otp_store = {}
GMAIL_USER = os.environ.get('GMAIL_USER')
BREVO_API_KEY = os.environ.get('BREVO_API_KEY')
def send_email(to_email, otp):
url = "https://api.brevo.com/v3/smtp/email"
headers = {
"api-key": BREVO_API_KEY,
"Content-Type": "application/json"
}
payload = {
"sender": {"name": "StepToDeen", "email": GMAIL_USER},
"to": [{"email": to_email}],
"subject": "Your StepToDeen Verification Code",
"htmlContent": f"""
StepToDeen
Hello,
Use the verification code below to continue:
{otp}
This code will expire in 5 minutes.
If you didn't request this code, please ignore this email.
© StepToDeen — Do not share this code with anyone.
"""
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code not in (200, 201):
raise Exception(response.text)
@app.route('/send-otp', methods=['POST'])
def send_otp():
data = request.json
email = data.get('email', '').strip().lower()
if not email:
return jsonify({"success": False, "message": "Email is required"}), 400
otp = str(random.randint(100000, 999999))
otp_store[email] = {"otp": otp, "expires": time.time() + 300}
try:
send_email(email, otp)
return jsonify({"success": True, "message": "Verification code sent to your email"})
except Exception as e:
return jsonify({"success": False, "message": f"Error: {str(e)}"}), 500
@app.route('/verify-otp', methods=['POST'])
def verify_otp():
data = request.json
email = data.get('email', '').strip().lower()
code = data.get('otp', '').strip()
record = otp_store.get(email)
if not record:
return jsonify({"success": False, "message": "Request a code first"}), 400
if time.time() > record['expires']:
del otp_store[email]
return jsonify({"success": False, "message": "Code expired, request a new one"}), 400
if record['otp'] == code:
del otp_store[email]
return jsonify({"success": True, "message": "Verification successful"})
return jsonify({"success": False, "message": "Invalid code"}), 400
@app.route('/')
def home():
return "OTP Server is running ✅"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)