Skip to content

Commit

Permalink
Pull Request from branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Lowkik-Sai committed Mar 24, 2024
1 parent 3ad19ae commit 4aa3e47
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 153 deletions.
32 changes: 23 additions & 9 deletions Backend/Controllers/Update_Profile_Controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
const Update_Profile_Module = require('../Modules/Update_Profile_Module');

const Update_Profile_Controller = async(req, res) => {
const User_Name = req.params.User_Name;
const { Email, Access_Token } = req.body;
const Update_Profile_response = await Update_Profile_Module(User_Name, Email, Access_Token);
if(Update_Profile_response.responseCode == 200){
res.status(200).json(Update_Profile_response.responseBody);
}
else{
res.status(100).json(Update_Profile_response.responseBody);
const Update_Profile_Controller = {
profile : async(req, res) => {
const User_Name = req.params.User_Name;
const { Email, Access_Token } = req.body;
const Update_Profile_response = await Update_Profile_Module.profile(User_Name, Email, Access_Token);
if(Update_Profile_response.responseCode == 200){
res.status(200).json(Update_Profile_response.responseBody);
}
else{
res.status(100).json(Update_Profile_response.responseBody);
}
},

password : async(req,res) => {
const User_Name = req.params.User_Name;
const { Password } = req.body;
const Update_Profile_response = await Update_Profile_Module.password(User_Name, Password);
if(Update_Profile_response.responseCode == 200){
res.status(200).json(Update_Profile_response.responseBody);
}
else{
res.status(100).json(Update_Profile_response.responseBody);
}
}
}

Expand Down
42 changes: 29 additions & 13 deletions Backend/Controllers/otpController.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
const {otpModule,otpGenerator} = require("../Modules/otpModule");

const otpController={
otpGenerate:async(req,res)=>{
const otpGenerate=await otpGenerator(req.params.User_Name);
if(otpGenerate.responseCode==200){
res.status(200).json(otpGenerate.responseBody)
}else{
//Email not exists or OTP sending failed
res.status(404).json(otpGenerate.responseBody)
otpGenerate: async (req, res) => {
try {
const otpGenerateResponse = await otpGenerator(req.params.User_Name);
console.log("OTP Generate Response :", otpGenerateResponse);

if (otpGenerateResponse.responseCode === 200) {
res.status(200).json(otpGenerateResponse);
} else {
res.status(404).json(otpGenerateResponse);
}
}
catch (error) {
console.error(error);
res.status(500).json({ responseBody: "Internal Server Error" });
}
},
otpVerify:async(req,res)=>{
const otpResponse = await otpModule(req)
if(otpResponse.responseCode == 200){
res.status(200).json(otpResponse.responseBody);
}
else{
res.status(100).json(otpResponse.responseBody);
try {
const otpResponse = await otpModule(req)
console.log("OTP Verify Response :", otpResponse);

if(otpResponse.responseCode == 200){
res.status(200).json(otpResponse);
}
else{
res.status(100).json(otpResponse);
}
}
catch (error) {
console.error(error);
res.status(500).json({ responseBody: "Internal Server Error" });
}

}
}

Expand Down
1 change: 0 additions & 1 deletion Backend/Controllers/sample

This file was deleted.

1 change: 0 additions & 1 deletion Backend/Middleware/sample

This file was deleted.

90 changes: 62 additions & 28 deletions Backend/Modules/Update_Profile_Module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const AWS = require('aws-sdk');
const crypto = require('crypto');
require('dotenv').config();

AWS.config.update({
Expand All @@ -12,35 +13,68 @@ const docClient = new AWS.DynamoDB.DocumentClient();
let responseCode = 200;
let responseBody = "";

const Update_Profile = async(User_Name, Email, Access_Token) => {
const params = {
TableName: "Auth",
Key: {
"User_Name": User_Name
},
UpdateExpression: "set Email = :x, Access_Token = :y",
ExpressionAttributeValues: {
":x": Email,
":y": Access_Token
}
};
const Update_Profile = {
profile : async(User_Name, Email, Access_Token) => {
const params = {
TableName: "Auth",
Key: {
"User_Name": User_Name
},
UpdateExpression: "set Email = :x, Access_Token = :y",
ExpressionAttributeValues: {
":x": Email,
":y": Access_Token
}
};

docClient.update(params, function(err, data) {
if(err){
responseCode = 100;
responseBody = "Error in Updating Profile";
}
else{
responseBody = "Successfully Updated Profile";
}
});

const response = {
responseCode,
responseBody
};

return response;
},

docClient.update(params, function(err, data) {
if(err){
responseCode = 100;
responseBody = "Error in Updating Profile";
}
else{
responseBody = "Successfully Updated Profile";
}
});

const response = {
responseCode,
responseBody
};

return response;
password : async(User_Name, Password) => {
const hashedPassword = crypto.createHash('sha256').update(Password).digest('hex');
const params = {
TableName: "Auth",
Key: {
"User_Name": User_Name
},
UpdateExpression: "set Password = :x",
ExpressionAttributeValues: {
":x": hashedPassword
}
};

docClient.update(params, function(err, data) {
if(err){
responseCode = 100;
responseBody = "Error in Updating Password";
}
else{
responseBody = "Successfully Updated Password";
}
});

const response = {
responseCode,
responseBody
};

return response;
},
}

module.exports = Update_Profile;
113 changes: 37 additions & 76 deletions Backend/Modules/otpModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ AWS.config.update({
var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" });
const docClient = new AWS.DynamoDB.DocumentClient();

let responseCode = 200;
let responseBody = "";
let responseCode = 100;
let responseBody = "Something went wrong";
let userName = "";



Expand All @@ -38,8 +39,6 @@ async function sendOTP(email,otp){
.then(msg => console.log(msg)) // logs response data
.catch(err => console.log(err)); // logs any error



}

async function otpGenerator(User_Name){
Expand All @@ -63,41 +62,22 @@ async function otpGenerator(User_Name){

let email=""

await ddb.getItem(parameters, (err, data) => {
if(err){
console.log(err);
responseCode = 404;
responseBody = "Error in Reading Database";
const response = {
responseCode,
responseBody
};
return response;
}
console.log(data)
if(data.Item.User_Name.S === User_Name){
email=data.Item.Email.S

sendOTP(email,otp)//Send OTP through Mailer

}else{
responseCode = 420;
responseBody = "No User Exists with Given User Name";
const response = {
responseCode,
responseBody
};
return response;
}
})
const data = await ddb.getItem(parameters).promise();

if (!data.Item || !data.Item.Email) {

return {
userName: User_Name,
responseCode: 404,
responseBody: "No User Exists with Given User Name"
};
}




email = data.Item.Email.S;
sendOTP(email, otp); // Send OTP through Mailer

const hashedOTP = await hash.create(otp);
console.log(`OTP : ${otp} and HashOTP : ${hashedOTP}`)
console.log(`OTP : ${otp} and HashOTP : ${hashedOTP}`);

const params = {
TableName: "Auth",
Expand All @@ -107,27 +87,24 @@ async function otpGenerator(User_Name){
UpdateExpression: "set OTP = :x",
ExpressionAttributeValues: {
":x": hashedOTP
}
};

await docClient.update(params, function(err, data) {
if(err){
responseCode = 100;
responseBody = "Error in Updating OTP";
return false;
}
else{
responseBody = "Successfully Updated OTP ";
responseCode = 200;
}
});
};

await docClient.update(params).promise();

return {
userName: User_Name,
responseCode: 200,
responseBody: "Successfully Updated OTP"
};


}catch(error){
responseBody = error;
responseCode = 400;
}
const response = {
userName,
responseCode,
responseBody
};
Expand All @@ -144,38 +121,22 @@ const otpModule = async(req) =>{
},
TableName: "Auth"
};

await ddb.getItem(params, async function(err, data) {
if(err){
console.log(err);
responseCode = 404;
responseBody = "Error in Reading Database";
const response = {
responseCode,
responseBody
};
return response;
}
else{
console.log(data.Item.OTP.S+" OTP : "+req.body.otp);
const isMatch = await hash.verify(data.Item.OTP.S,req.body.otp);
if(isMatch){
responseCode=200;
responseBody="Successfully Verified"
}
else{
responseCode=404;
responseBody="Invalid OTP"
}
console.log(responseBody)
}
});

const data = await ddb.getItem(params).promise();
console.log(data.Item.OTP.S+" OTP : "+req.body.otp);
const isMatch = await hash.verify(data.Item.OTP.S,req.body.otp);
if(isMatch){
responseCode=200;
responseBody="Successfully Verified"
}
else{
responseCode=404;
responseBody="Invalid OTP"
}
const response = {
responseCode,
responseBody
};

return response;
}

Expand Down
1 change: 0 additions & 1 deletion Backend/Modules/sample

This file was deleted.

11 changes: 8 additions & 3 deletions Backend/Routers/Update_Profile_Router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const Update_Profile_Controller = require('../Controllers/Update_Profile_Controller');
const express = require('express');

const express = require('express');
const router = express.Router();

router.post("/updateprofile/:User_Name", (req, res) => {
Update_Profile_Controller(req, res);
router.post("/updateprofile/:type/:User_Name", (req, res) => {
const type = req.params.type;
if(type=="profile"){
Update_Profile_Controller.profile(req, res);
}else if(type=="password"){
Update_Profile_Controller.password(req, res);
}
})

module.exports = router;
1 change: 0 additions & 1 deletion Backend/Routers/sample

This file was deleted.

2 changes: 1 addition & 1 deletion Backend/mainRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Update_Profile = require('./Routers/Update_Profile_Router');
const Login = require('./Routers/Login_Router');
const Register = require('./Routers/Register_Router');
const commit = require('./Routers/Commit_Route');
const otp = require("./Routers/otpRoute")
const otp = require("./Routers/otpRoute");

//Middlewares
const webTokenValidator = require('./Middleware/webTokenValidator');
Expand Down
Loading

0 comments on commit 4aa3e47

Please sign in to comment.