Skip to content

Commit

Permalink
added profile generation code after verifyEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiraj-ku committed Sep 7, 2024
1 parent 8f807cb commit a4c1bb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
} = require("../services/emailServices");
const { verifyEmail, verifyPhone } = require("../utils/isContactsValid");
const cookieToken = require("../utils/cookieToken");
const generateUserProfileImage = require("../utils/ generateUserProfileImage");
const { validateUsersChoice } = require("../helpers/validateUserChoice");
const queueEmailSending = require("../services/emailsenderProducer");

Expand Down Expand Up @@ -56,7 +57,7 @@ module.exports.register = async (req, res) => {
}

// Generate verification code and save this to redis with TTL of 3 minutes
const verificationCode = await storeuser(name, email);
await storeuser(name, email);

// Queue email for sending verification code
const emailContent = await sendVerificationCode(email);
Expand Down Expand Up @@ -166,6 +167,26 @@ module.exports.verifyEmail = async (req, res) => {
// mark the email verified as true
user.emailVerified = true;

try {
// Generate user profile photo
const profileImagePath = await generateUserProfileImage(user.name);

// upload to s3
const profileUrl = await uplaodToS3(
profileImagePath,
`${user.name}-profile.jpg`
);

// update the user avatar image
user.avatar = profileUrl;
} catch (error) {
console.error(`Error generating or uploading profile image:`, error);
return res.status(500).json({
message:
"Server error while generating profile image. Please try again.",
});
}

// save the changes to db
await user.save();

Expand All @@ -181,6 +202,7 @@ module.exports.verifyEmail = async (req, res) => {
Name: user.name,
Email: user.email,
emailVerified: user.emailVerified,
avatar: user.avatar,
},
});

Expand Down
6 changes: 0 additions & 6 deletions src/utils/uploadToS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ const { s3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const fs = require("fs");
const path = require("path");

const profilePhotoFolder = path.join(__dirname, "../../profile_photo");

const fileName = `${username}-profile-${Date.now()}.jpg`;

const localFilePath = path.join(profilePhotoFolder, fileName);

// Create a S3 client using aws sdk
const s3Client = {
region: process.env.AWS_REGION,
Expand Down

0 comments on commit a4c1bb7

Please sign in to comment.