Skip to content

Commit 2726ae6

Browse files
Version 3.1.3
Fix UserController create user password var Fix AuthController error var Refactor MailerService
1 parent bc032c1 commit 2726ae6

File tree

6 files changed

+62
-70
lines changed

6 files changed

+62
-70
lines changed

package.json

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node_api",
3-
"version": "3.0.4",
3+
"version": "3.1.3",
44
"description": "Node API is production ready and open source project in Node, Express, MongoDB",
55
"scripts": {
66
"start": "node server.js"
@@ -18,6 +18,6 @@
1818
"nodemailer": "^4.2.0"
1919
},
2020
"engines": {
21-
"node": "11.10.0"
21+
"node": "14.0.0"
2222
}
2323
}

server.js

100755100644
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"use strict";
2-
31
const mongoose = require("mongoose");
4-
const http = require("http");
52
const expressServer = require("./src/config/express");
63
const cluster = require("cluster");
74
const OS = require("os");
@@ -14,30 +11,30 @@ class App {
1411
this.buildCluster(workers, autoScale);
1512
}
1613

17-
database() {
14+
database = () => {
1815
mongoose.connect(databaseUrl, {
1916
useCreateIndex: true,
2017
useNewUrlParser: true,
2118
useUnifiedTopology: true
2219
});
2320

24-
mongoose.connection.on("connected", function() {
21+
mongoose.connection.on("connected", () => {
2522
console.log("Connected Database");
2623
});
2724

28-
process.on("SIGINT", function() {
29-
mongoose.connection.close(function() {
25+
process.on("SIGINT", () => {
26+
mongoose.connection.close(() => {
3027
console.log("Close database connection");
3128
process.exit(0);
3229
});
3330
});
3431

35-
mongoose.connection.on("error", function(error) {
32+
mongoose.connection.on("error", error => {
3633
console.log("Connection Error: " + error);
3734
});
38-
}
35+
};
3936

40-
buildCluster(workers, autoScale) {
37+
buildCluster = (workers, autoScale) => {
4138
if (autoScale) {
4239
workers = this.autoScale(workers);
4340
}
@@ -60,13 +57,13 @@ class App {
6057
console.log(`Server running on port ${PORT}`);
6158
});
6259
}
63-
}
60+
};
6461

65-
autoScale(workersByCpu) {
62+
autoScale = workersByCpu => {
6663
const cpus = OS.cpus().length;
6764
const workers = cpus / workersByCpu;
6865
return workers;
69-
}
66+
};
7067
}
7168

7269
/**

src/app/Controllers/AuthController.js

100755100644
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
"use strict";
2-
31
const mongoose = require("mongoose");
42
const bcrypt = require("bcrypt");
53
const jwt = require("jsonwebtoken");
64
const model = mongoose.model("User");
75
const { secret } = require("../Constants/auth");
86

97
class AuthController {
10-
async login(req, res) {
8+
login = async (req, res) => {
119
try {
1210
const user = await model.findOne({ email: req.body.email });
1311
const match = await bcrypt.compareSync(req.body.password, user.password);
1412

1513
let token;
1614
if (!match) {
17-
res.status(401).send({ error: error, message: "Password mismatch" });
15+
res.status(401).send({ error: "error", message: "Password mismatch" });
1816
token = jwt.sign({ user_id: user._id }, secret, {
1917
expiresIn: "3h"
2018
});
@@ -35,11 +33,11 @@ class AuthController {
3533
}
3634
};
3735

38-
async verifyToken(req, res, next) {
39-
const TOKEN = req.get("Authorization");
40-
if (TOKEN) {
36+
verifyToken = (req, res, next) => {
37+
const token = req.get("Authorization");
38+
if (token) {
4139
try {
42-
let decoded = jwt.verify(token, SECRET);
40+
let decoded = jwt.verify(token, secret);
4341
req.user = decoded;
4442
next();
4543
} catch (error) {

src/app/Controllers/ProjectController.js

100755100644
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
2-
31
const mongoose = require("mongoose");
42
const model = mongoose.model("Project");
53

64
class ProjectController {
7-
async getAllProjects(req, res) {
5+
async getAllProjects(_, res) {
86
try {
97
const projects = await model.find({});
108
res.status(200).json(projects);

src/app/Controllers/UserController.js

100755100644
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
1-
"use strict";
2-
31
const mongoose = require("mongoose");
4-
const mailService = require("../Services/Mail");
52
const bcrypt = require("bcrypt");
63
const { rounds } = require("../Constants/auth");
74
const model = mongoose.model("User");
8-
const transporter = mailService.config();
95

10-
class UserController {
11-
constructor() {
12-
this.createUser = this.createUser.bind(this);
13-
this.updateUser = this.updateUser.bind(this);
14-
}
6+
const MailerService = require("../Services/Mail");
157

16-
hashPassword(password) {
8+
class UserController {
9+
hashPassword = password => {
1710
const saltRounds = bcrypt.genSaltSync(rounds);
1811
const hashedPassword = bcrypt.hashSync(password, saltRounds);
1912

2013
return hashedPassword;
2114
};
2215

23-
sendWelcomeEmail(email) {
24-
const content = mailService.content(email);
25-
transporter.sendMail(content);
16+
sendWelcomeEmail = email => {
17+
const content = MailerService.welcomeMailContent(email);
18+
MailerService.sendMail(content);
2619
};
2720

28-
async getAllUsers(req, res) {
21+
getAllUsers = async (_, res) => {
2922
try {
3023
const users = await model.find({});
3124
res.status(200).json(users);
3225
} catch (error) {
3326
res.status(500).json(error);
3427
}
35-
}
28+
};
3629

37-
async createUser(req, res) {
30+
createUser = async (req, res) => {
3831
const user = req.body;
3932
try {
4033
user.password = this.hashPassword(user.password);
@@ -48,18 +41,18 @@ class UserController {
4841
}
4942
};
5043

51-
async updateUser(req, res) {
44+
updateUser = async (req, res) => {
5245
const user = req.body;
5346
try {
54-
user.password = this.hashPassword(password);
47+
user.password = this.hashPassword(user.password);
5548
await model.findByIdAndUpdate(req.params.id, user);
5649
res.status(200).json(user);
5750
} catch (error) {
5851
res.status(404).json(error);
5952
}
6053
};
6154

62-
async searchUserById(req, res) {
55+
searchUserById = async (req, res) => {
6356
const { id } = req.params;
6457
try {
6558
const user = await model.findById(id);
@@ -69,7 +62,7 @@ class UserController {
6962
}
7063
};
7164

72-
async deleteUserById(req, res) {
65+
deleteUserById = async (req, res) => {
7366
const { id } = req.params;
7467
try {
7568
await model.remove({ _id: id });

src/app/Services/Mail.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
const nodemailer = require('nodemailer');
1+
const nodemailer = require("nodemailer");
22

3-
let mailService = {};
3+
class MailerService {
4+
constructor() {
5+
this.transport = this.config();
6+
}
7+
8+
onfig = () =>
9+
nodemailer.createTransport({
10+
host: "smtp.gmail.com",
11+
auth: {
12+
user: "rest.api.node@gmail.com",
13+
pass: "pnzkxchwwqddzhve"
14+
}
15+
});
416

5-
mailService.config = () => (
6-
nodemailer.createTransport({
7-
host: 'smtp.gmail.com',
8-
auth: {
9-
user: 'rest.api.node@gmail.com',
10-
pass: 'pnzkxchwwqddzhve'
11-
}
12-
})
13-
);
17+
sendMail = mail => {
18+
this.transport.sendMail(mail);
19+
};
1420

15-
mailService.content = userMail => (
16-
{
21+
welcomeMailContent = userMail => ({
1722
from: '"Node API" <rest-api-node@gmail.com>',
1823
to: userMail,
19-
subject: 'Welcome to Node API',
20-
html: '<p>Welcome to Node API!</p>'
21-
+'<p>This API is developed to provide Front End developers with a server and database configurations for connection testing and to provide a visualization of the data streamlining for the development process. The API provides HTTP methods via AJAX requests to collect, insert and update the given data.</p>'
22-
+'Github: <a href="https://github.com/renanlopescoder/rest-api-node">rest-api-node</a>'
23-
+'<br><br>Happy Hack!,'
24-
+'<br>Node API Team 🤖'
25-
+'<br><br><img src="http://pluspng.com/img-png/github-octocat-logo-vector-png--896.jpg" alt="Github Logo" height="82" width="82">'
26-
+'<img src="https://raw.githubusercontent.com/renanlopescoder/rest-api-node/master/logo.png" alt="Node Logo" height="65" width="150">'
27-
}
28-
);
24+
subject: "Welcome to Node API",
25+
html:
26+
"<p>Welcome to Node API!</p>" +
27+
"<p>This API is developed to provide Front End developers with a server and database configurations for connection testing and to provide a visualization of the data streamlining for the development process. The API provides HTTP methods via AJAX requests to collect, insert and update the given data.</p>" +
28+
'Github: <a href="https://github.com/renanlopescoder/rest-api-node">rest-api-node</a>' +
29+
"<br><br>Happy Hack!," +
30+
"<br>Node API Team 🤖" +
31+
'<br><br><img src="http://pluspng.com/img-png/github-octocat-logo-vector-png--896.jpg" alt="Github Logo" height="82" width="82">' +
32+
'<img src="https://raw.githubusercontent.com/renanlopescoder/rest-api-node/master/logo.png" alt="Node Logo" height="65" width="150">'
33+
});
34+
}
2935

30-
module.exports = mailService
36+
module.exports = new MailerService();

0 commit comments

Comments
 (0)