Skip to content

Commit 43a80e3

Browse files
authored
Fixed: /sendFeedback route working
Fixed: /sendFeedback route working
2 parents 30862a8 + 8799c0c commit 43a80e3

File tree

7 files changed

+535
-87
lines changed

7 files changed

+535
-87
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
node_modules
22

33
## sensitive information
4-
constants.js
4+
.env

Mailer/transporter.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const nodemailer = require('nodemailer')
1+
const nodemailer = require("nodemailer");
22

33
// constants
4-
const { SERVICE, EMAIL, PASSWORD } = require('../constants')
4+
const { SERVICE, EMAIL, PASSWORD } = require("../constants");
55

66
const transporter = nodemailer.createTransport({
7-
service: SERVICE,
8-
auth: {
9-
user: EMAIL,
10-
pass: PASSWORD
11-
}
12-
})
13-
14-
15-
module.exports = transporter
7+
service: SERVICE,
8+
auth: {
9+
user: EMAIL,
10+
pass: PASSWORD,
11+
},
12+
});
13+
14+
module.exports = transporter;

ValidateUser/validateuser.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
const express = require('express')
2-
1+
const express = require("express");
32

43
// TOKEN
5-
const { TOKEN } = require('../constants')
6-
7-
const verifyToken = (req,res,next)=>{
4+
const { TOKEN } = require("../constants");
85

9-
if(!req.headers['authorization']) return next(new Error('User token not found'))
6+
const verifyToken = (req, res, next) => {
7+
if (!req.headers["authorization"])
8+
return next(new Error("User token not found"));
109

11-
const authHeader = req.headers['authorization']
12-
const token = authHeader.split(' ')[1]
13-
if(token==TOKEN) next()
14-
else
15-
return next(new Error('Unauthorized User.'))
16-
17-
}
10+
const authHeader = req.headers["authorization"];
11+
const token = authHeader.split(" ")[1];
12+
if (token == TOKEN) next();
13+
else return next(new Error("Unauthorized User."));
14+
};
1815

19-
module.exports = verifyToken
16+
module.exports = verifyToken;

constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// email credentials
2+
const SERVICE = process.env.EMAIL_SERVICE;
3+
const EMAIL = process.env.EMAIL;
4+
const PASSWORD = process.env.PASSWORD;
5+
6+
// for feedback
7+
const EMAILTO = process.env.EMAIL_TO;
8+
9+
// google photos
10+
const ALBUMID = process.env.ALBUM_ID;
11+
12+
// token
13+
const TOKEN = process.env.TOKEN;
14+
15+
module.exports = { SERVICE, EMAIL, PASSWORD, EMAILTO, ALBUMID, TOKEN };

index.js

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
1-
const express = require('express')
2-
const cors = require('cors')
3-
const bodyParser = require('body-parser')
1+
const express = require("express");
2+
const cors = require("cors");
3+
const bodyParser = require("body-parser");
44
const app = express();
5+
require("dotenv").config();
56

67
// user verification
7-
const verifyToken = require('./ValidateUser/validateuser')
8+
const verifyToken = require("./ValidateUser/validateuser");
89

910
// google photos
10-
const { getAlbum } = require('./GooglePhotos/googlePhotos')
11+
const { getAlbum } = require("./GooglePhotos/googlePhotos");
1112

1213
// mail
13-
const transporter = require('./Mailer/transporter')
14-
const sendEmail = require('./Mailer/mail')
14+
const transporter = require("./Mailer/transporter");
15+
const sendEmail = require("./Mailer/mail");
1516

1617
// constants
17-
const { EMAIL, EMAILTO, ALBUMID } = require('./constants')
18+
const { EMAIL, EMAILTO, ALBUMID } = require("./constants");
1819

1920
// middlewares
2021
app.use(bodyParser.urlencoded({ extended: true }));
21-
app.use(bodyParser.json())
22-
app.use(cors())
22+
app.use(bodyParser.json());
23+
app.use(cors());
2324

2425
app.use(express.static("public"));
2526

2627
/**
2728
* To change email id (to be used for sending emails)
28-
*
29+
*
2930
* 1. Go to account setting of your GMAIL account.
3031
* 2. Then go to 'allow less secure apps..'.
3132
* 3. Generate a new password and use that here to allow nodemailer to logging in your gmail account, skipping 2 step verification step.
32-
*
33+
*
3334
* Reference:
3435
* https://stackoverflow.com/questions/26196467/sending-email-via-node-js-using-nodemailer-is-not-working
35-
*
36+
*
3637
*/
3738

38-
3939
/**
4040
* this endpoint is for sending user's credentials when a new account is created from admin panel.
4141
*/
4242

43-
app.post("/sendMail", verifyToken ,async (req, res, next) => {
44-
45-
let mailOptions = {
46-
from: EMAIL,
47-
to: req.body.email || EMAIL,
48-
subject: 'Technocrats Member Profile Credentials',
49-
html: `
43+
app.post("/sendMail", verifyToken, async (req, res, next) => {
44+
let mailOptions = {
45+
from: EMAIL,
46+
to: req.body.email || EMAIL,
47+
subject: "Technocrats Member Profile Credentials",
48+
html: `
5049
<h1>Welcome to Technocrats Robotics</h1>
5150
<p>Click on the&nbsp;<strong>Member Login</strong> button at the bottom footer of the web app.</p>
5251
<p><span style="color: #0000ff;">Following are your credentials to customize your profile on the web app:<br /> <span style="color: #ff0000;"><strong>Username:</strong> ${req.body.data.username}</span><br /><span style="color: #ff0000;"><strong>Password:</strong> ${req.body.data.password}<br /></span>Whosoever holds these credentials, if he be worthy, shall possess the power of a Member.</span></p>
@@ -55,73 +54,70 @@ app.post("/sendMail", verifyToken ,async (req, res, next) => {
5554
<blockquote>
5655
<p><strong>This is only a foretaste of what is to come, and only the shadow of what is going to be. <em>~ Alan Turing</em></strong></p>
5756
</blockquote>
58-
`
59-
};
60-
61-
let response = await sendEmail(transporter, mailOptions,next)
57+
`,
58+
};
6259

63-
if (response)
64-
res.status(200).send({ message: "Sent" });
65-
})
60+
let response = await sendEmail(transporter, mailOptions, next);
6661

62+
if (response) res.status(200).send({ message: "Sent" });
63+
});
6764

6865
/**
69-
* endpoint to send feedback submitted by user in Contact Us form to TCR's official mail id
66+
* endpoint to send feedback submitted by user in Contact Us form to TCR's official mail id
7067
*/
71-
app.post("/sendFeedback", verifyToken ,async (req, res, next) => {
72-
73-
let mailOptions = {
74-
from: EMAIL,
75-
to: EMAILTO,
76-
subject: 'Got a Feedback !!',
77-
html: `
68+
app.post("/sendFeedback", verifyToken, async (req, res, next) => {
69+
let mailOptions = {
70+
from: EMAIL,
71+
to: EMAILTO,
72+
subject: "Got a Feedback !!",
73+
html: `
7874
<h1><u>Feedback</u></h1>
7975
<p><b><u>Name:</u></b> ${req.body.data.name}</p>
8076
<p><b><u>Email:</u></b> ${req.body.data.email}</p>
8177
<p><b><u>Contact:</u></b> ${req.body.data.contact}</p>
8278
<p><b><u>Message:</u></b> ${req.body.data.message}</p>
83-
`
84-
};
85-
86-
const response = await sendEmail(transporter, mailOptions, next)
79+
`,
80+
};
8781

88-
if (response)
89-
res.status(200).send({ message: "Sent" });
90-
})
82+
try {
83+
const response = await sendEmail(transporter, mailOptions, next);
9184

85+
if (response) res.status(200).send({ message: "Sent" });
86+
} catch (err) {
87+
next(err);
88+
}
89+
});
9290

9391
/**
9492
* get the album id from google photos and pass it in the function getAlbum(<id>)
95-
*
93+
*
9694
*/
9795

98-
app.get("/gallery", verifyToken,async (req, res, next) => {
99-
const result = await getAlbum(ALBUMID)
100-
res.json(result)
101-
})
96+
app.get("/gallery", verifyToken, async (req, res, next) => {
97+
const result = await getAlbum(ALBUMID);
98+
res.json(result);
99+
});
102100

103101
/*
104102
* information
105103
*/
106104
app.get("/", (req, res, next) => {
107-
res.sendFile(__dirname + "/" + "Information/information.html")
108-
})
105+
res.sendFile(__dirname + "/" + "Information/information.html");
106+
});
109107

110108
// error handler
111109
app.use((err, req, res, next) => {
112-
res.status(err.status || 500)
113-
res.send({
114-
error: {
115-
status: err.status || 500,
116-
message: err.message
117-
}
118-
})
119-
})
120-
110+
res.status(err.status || 500);
111+
res.send({
112+
error: {
113+
status: err.status || 500,
114+
message: err.message,
115+
},
116+
});
117+
});
121118

122119
var port = process.env.PORT || 5000;
123120

124-
125121
app.listen(port, function () {
126-
console.log('Express server listening on port ' + port);
122+
console.log("Express server listening on port " + port);
127123
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"axios": "^0.24.0",
2121
"body-parser": "^1.19.0",
2222
"cors": "^2.8.5",
23+
"dotenv": "^16.0.1",
2324
"express": "^4.17.2",
2425
"nodemailer": "^6.7.1"
2526
}

0 commit comments

Comments
 (0)