RegisterController.php

This handles user registration by creating a user account, generating a verification code, sending an email for email verification, initializing user profile data, and writing bookmark and map file.

registerUser

public function registerUser(Request $request)
{
    // Extract user input from the request
    $username = $request->input('username');
    $email = $request->input('email');
    $password = $request->input('password');
    // Generate a unique verification code using md5 and uniqid
    $verificationCode = md5(uniqid(rand(), true));
    $verified = false;
    // Create a new UserAccount model instance and save it to the database
    $userAccount = new UserAccount([
        'username' => $username,
        'email' => $email,
        'password' => $password,
        'verification_code' => $verificationCode,
        'verified' => $verified,
    ]);
    $userAccount->save();
    // Create a new UserProfile associated with the user account
    $userProfile = new UserProfile([
        'user_id' => $userAccount->id,
    ]);
    $userProfile->save();
    // Create a directory and a JSON file for bookmarks
    $directory = 'Bookmark';
    $fileBookmark = $directory . '/' . $userAccount->id . '.json';
    if (!file_exists($directory)) {
        mkdir($directory, 0777, true);
    }
    $json = [
        'USER_ID' => $userAccount->id,
        'DATA' => [],
    ];
    $prettyPrintedJson = json_encode($json, JSON_PRETTY_PRINT);
    $file = fopen($fileBookmark, 'w');
    if ($file) {
        fwrite($file, $prettyPrintedJson);
        fclose($file);
    }
    // Create a directory and a JSON file for maps
    $directory = 'Map';
    $fileMap = $directory . '/' . $userAccount->id . '.json';
    if (!file_exists($directory)) {
        mkdir($directory, 0777, true);
    }
    $json = [
        'USER_ID' => $userAccount->id,
        'DATA' => [],
    ];
    $prettyPrintedJson = json_encode($json, JSON_PRETTY_PRINT);
    $file = fopen($fileMap, 'w');
    if ($file) {
        fwrite($file, $prettyPrintedJson);
        fclose($file);
    }
    // Send an email for email verification
    $subject = "Email verification";
    $FRONTEND_URL = getenv('FRONTEND_URL');
    $body = "<div style='display:block'>Click the following link to verify your email:&nbsp;" . $FRONTEND_URL . "/verify" . "/" . $verificationCode . "</div>";
    Mail::to($email)->send(new EmailContent($subject, $body));
    // Prepare and return a success response
    $response = [
        'SUCCESS' => 1,
    ];
    return $response;
}

To explain what this registerUser method does:

  1. Request Handling:

    • It takes a Request object as a parameter, which presumably contains user input data from a registration form.

  2. User Input Extraction:

    • It extracts the username, email, and password from the request.

  3. Generate Verification Code:

    • It generates a unique verification code using md5(uniqid(rand(), true)). This code will be used for email verification.

  4. Database Operations:

    • It creates a new UserAccount model instance with the extracted user data, including the verification code and a 'verified' flag set to false.

    • It saves the UserAccount instance to the database.

    • It creates a new UserProfile associated with the user account and saves it to the database.

  5. Bookmark JSON File Handling:

    • It creates a directory named 'Bookmark' if it doesn't exist.

    • It creates a JSON file within the 'Bookmark' directory, named after the user's ID, containing an initial structure with an empty array for data.

  6. Map JSON File Handling:

    • It creates a directory named 'Map' if it doesn't exist.

    • It creates a JSON file within the 'Map' directory, named after the user's ID, containing an initial structure with an empty array for data.

  7. Email Verification:

    • It sends an email for email verification using Laravel's Mail facade.

    • The email contains a verification link with the generated verification code appended to the frontend URL.

  8. Response Preparation:

    • It prepares a success response in the form of an associative array with a 'SUCCESS' key set to 1.

  9. Response Return:

    • It returns the prepared success response.

Last updated