From 4cd5bb30d68db2fbae445f3501979e37de341ab5 Mon Sep 17 00:00:00 2001 From: PlainsTunneler Date: Mon, 23 Jan 2023 20:02:41 +0530 Subject: [PATCH 1/2] Created mail endpoint --- Backend/service/mail.js | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Backend/service/mail.js diff --git a/Backend/service/mail.js b/Backend/service/mail.js new file mode 100644 index 0000000..ad929b4 --- /dev/null +++ b/Backend/service/mail.js @@ -0,0 +1,65 @@ +const AWS = require("aws-sdk"); +AWS.config.update({ + region: "ap-south-1", +}); +const util = require("../utils/util"); +const bcrypt = require("bcryptjs"); +const auth = require("../utils/auth"); +const dynamodb = new AWS.DynamoDB.DocumentClient(); +const userTable = "User_Data"; +const amazonSes = new AWS.SES.DocumentClient(); + +async function mail(requestBody) { + const username = requestBody.username; + const token = requestBody.token; + const to = requestBody.to; + const body = requestBody.body; + if (!username || !token || !to || !body) { + return util.buildResponse(401, { + message: "Missing fields!", + }); + } + const dynamoUser = await getUser(username); + if (!dynamoUser || !dynamoUser.username) { + return util.buildResponse(403, { + message: "user does not exist", + }); + } + if (!bcrypt.compareSync(password, dynamoUser.password)) { + return util.buildResponse(403, { + message: "password is incorrect", + }); + } + + const sendMailResponse = await sendMail(user); + if (!sendMailResponse) { + return util.buildResponse(503, { + message: "Server error.", + }); + } + + return util.buildResponse(200, response); +} + +async function getUser(username) { + const params = { + TableName: userTable, + Key: { + username: username, + }, + }; + return await dynamodb + .get(params) + .promise() + .then( + (response) => { + console.log("Response received!", response.Item); + return response.Item; + }, + (error) => { + console.error("There is an error: ", error); + } + ); +} + +module.exports.mail = mail; From 346c7ecea81cb56dc0efdb425f7afd3f11bc9189 Mon Sep 17 00:00:00 2001 From: PlainsTunneler Date: Tue, 24 Jan 2023 13:53:12 +0530 Subject: [PATCH 2/2] Email service backend --- Backend/index.js | 13 ++++++------ Backend/service/mail.js | 44 +++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Backend/index.js b/Backend/index.js index 0172bfe..ed4ba8e 100644 --- a/Backend/index.js +++ b/Backend/index.js @@ -3,6 +3,7 @@ const loginService = require("./service/login"); const verifyService = require("./service/verify"); const updateService = require("./service/update"); const scoreService = require("./service/score"); +const mailService = require("./service/mail"); const util = require("./utils/util"); const healthPath = "/health"; @@ -11,42 +12,40 @@ const loginPath = "/login"; const verifyPath = "/verify"; const scorePath = "/score"; const updatePath = "/update"; +const mailPath = "/sendmail"; exports.handler = async (event) => { console.log("Request Event : ", event); let response; switch (true) { case event.httpMethod === "GET" && event.path === healthPath: - console.log("healthEvent"); response = util.buildResponse(200); break; case event.httpMethod === "POST" && event.path === registerPath: - console.log("registerEvent"); const registerBody = JSON.parse(event.body); response = await registerService.register(registerBody); break; case event.httpMethod === "POST" && event.path === loginPath: - console.log("loginEvent"); const loginBody = JSON.parse(event.body); response = await loginService.login(loginBody); break; case event.httpMethod === "POST" && event.path === verifyPath: - console.log("verifyEvent"); const verifyBody = JSON.parse(event.body); response = verifyService.verify(verifyBody); break; case event.httpMethod === "PUT" && event.path === updatePath: - console.log("updateEvent"); const updateBody = JSON.parse(event.body); response = updateService.update(updateBody); break; case event.httpMethod === "POST" && event.path === scorePath: - console.log("scoreEvent"); const scoreBody = JSON.parse(event.body); response = scoreService.score(scoreBody); break; + case event.httpMethod === "POST" && event.path === mailPath: + const emailBody = JSON.parse(event.body); + response = mailService.mail(emailBody); + break; default: - console.log("nullEvent"); response = util.buildResponse(404, "404 Not Found!"); } return response; diff --git a/Backend/service/mail.js b/Backend/service/mail.js index ad929b4..f587f5e 100644 --- a/Backend/service/mail.js +++ b/Backend/service/mail.js @@ -7,14 +7,14 @@ const bcrypt = require("bcryptjs"); const auth = require("../utils/auth"); const dynamodb = new AWS.DynamoDB.DocumentClient(); const userTable = "User_Data"; -const amazonSes = new AWS.SES.DocumentClient(); +const amazonSes = new AWS.SES({ region: "ap-south-1" }); async function mail(requestBody) { - const username = requestBody.username; - const token = requestBody.token; const to = requestBody.to; + const subject = requestBody.subject; const body = requestBody.body; - if (!username || !token || !to || !body) { + const username = requestBody.username; + if (!username || !to || !body) { return util.buildResponse(401, { message: "Missing fields!", }); @@ -25,20 +25,34 @@ async function mail(requestBody) { message: "user does not exist", }); } - if (!bcrypt.compareSync(password, dynamoUser.password)) { - return util.buildResponse(403, { - message: "password is incorrect", + const params = { + Source: "hithu203@gmail.com", + Destination: { + ToAddresses: [to], + }, + Message: { + Body: { + Text: { + Data: body, + }, + }, + Subject: { + Data: subject, + }, + }, + }; + try { + const result = await amazonSes.sendEmail(params).promise(); + console.log(result); + return util.buildResponse(200, { + message: "Email sent successfully", }); - } - - const sendMailResponse = await sendMail(user); - if (!sendMailResponse) { - return util.buildResponse(503, { - message: "Server error.", + } catch (error) { + console.log(error); + return util.buildResponse(500, { + message: "Please enter verified email address", }); } - - return util.buildResponse(200, response); } async function getUser(username) {