Zyron-Tech / dummy-backend

A PHP backend with email OTP authentication using PHPMailer and PostgreSQL. It provides APIs for user registration, login, and OTP verification, designed for seamless integration with frontend applications.
https://dummy-backend-gyc3.onrender.com/
MIT License
2 stars 0 forks source link

OTP error after its send success #2

Closed TonyFred-code closed 2 months ago

TonyFred-code commented 2 months ago

Issue: I'm encountering an issue where the OTP email is successfully sent, but I get a JSON parsing error in the frontend with the message:

csharp Copy code Syntax error: Unexpected token 'O', 'OTP email' is not valid JSON The issue arises because the sendOtpEmail function in the mail.php script outputs a string (like "OTP email sent successfully.") directly, which breaks the JSON response from the PHP signup script.

Suggested Fix: To resolve this, the sendOtpEmail function should return a true or false status instead of outputting any messages directly. Here’s the suggested fix:

  1. Update sendOtpEmail Function: Replace the echo statements in mail.php with return true for success and return false for failure, like this:

php Copy code <?php require DIR . '/../vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;

function sendOtpEmail($email, $name, $otp) { $mail = new PHPMailer(true);

try {
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'markfestus24@gmail.com';
    $mail->Password = 'your-app-password'; // Gmail App Password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    // Sender and recipient settings
    $mail->setFrom('markfestus24@gmail.com', 'Dummy Backend');
    $mail->addAddress($email, $name);

    // Email content
    $mail->isHTML(true);
    $mail->Subject = 'Your OTP Code';
    $mail->Body = '<div style="font-family: Arial, sans-serif; text-align: center; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
        <h2 style="color: #4CAF50;">Your OTP Code</h2>
        <p style="font-size: 24px; font-weight: bold; color: #333; margin: 10px 0;">' . $otp . '</p>
        <hr style="border: 0; border-top: 1px solid #ddd; margin: 20px 0;">
        <p style="font-size: 12px; color: #888;">Credit: <a href="mailto:zyrontech101@gmail.com" style="color: #4CAF50; text-decoration: none;">Zyron-Tech</a></p>
    </div>';

    // Send email
    $mail->send();
    return true; // Return success
} catch (Exception $e) {
    // Log or return error information
    error_log("Email send error: " . $mail->ErrorInfo);
    return false; // Return failure
}

}

  1. Update signup.php to Handle the Email Send Status: Modify the signup script to check the return status of the sendOtpEmail function, like this:

php Copy code if ($result) { // Send OTP email if (sendOtpEmail($email, $username, $otp)) { echo json_encode(['status' => 'success', 'message' => 'Signup successful! Please check your email for the OTP.']); } else { echo json_encode(['status' => 'error', 'message' => 'Signup successful but failed to send OTP email.']); } } else { echo json_encode(['status' => 'error', 'message' => 'Failed to register user']); } This should prevent any non-JSON output from breaking the response and improve error handling during email sending.

Zyron-Tech commented 2 months ago

Issue: JSON Parsing Error in Frontend Due to Direct Output from sendOtpEmail

Comment by TonyFred-code:
While testing the OTP functionality, the OTP email sends successfully, but there's a JSON parsing error in the frontend:

Syntax error: Unexpected token 'O', 'OTP email' is not valid JSON

This issue occurs because the sendOtpEmail function in the mail.php script outputs a string directly (e.g., "OTP email sent successfully."), causing the JSON response from the signup script to break.

Fix Implemented

Issue Cause:
The sendOtpEmail function was outputting direct messages, interfering with the JSON format expected by the frontend.

Solution:
I modified the sendOtpEmail function to return true or false based on the success or failure of sending the email, instead of outputting messages directly. Additionally, we updated the signup.php script to handle these return values properly.

Changes Made:

  1. Updated sendOtpEmail Function

    • Replaced echo statements with return true; for success and return false; for failure.
  2. Modified signup.php Script

    • Handled the return value of sendOtpEmail to ensure only proper JSON responses are sent back to the frontend.
    • If the email fails to send, the response indicates that the signup was successful, but the OTP email sending failed.

Updated Code:

Here's the modified code for signup.php:


if ($result) {
    // Send OTP email
    if (sendOtpEmail($email, $username, $otp)) {
        echo json_encode(['status' => 'success', 'message' => 'Signup successful! Please check your email for the OTP.']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Signup successful but failed to send OTP email.']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Failed to register user']);
}