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
registerUserpublic 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: " . $FRONTEND_URL . "/verify" . "/" . $verificationCode . "</div>";
Mail::to($email)->send(new EmailContent($subject, $body));
// Prepare and return a success response
$response = [
'SUCCESS' => 1,
];
return $response;
}Last updated