Skip to content

Commit dd9c393

Browse files
committed
adding bcrypt and signup module
1 parent 4ab1b5b commit dd9c393

File tree

9 files changed

+763
-22
lines changed

9 files changed

+763
-22
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A simple and structured way boilerplate for Node with MySQL, equipped with MVC layer model with basic validation of schema and common error handler, authentication and easily pluggable code base.
44

5-
This Boilerplate have a basic CRUD operation with MySQL, authetication of API endpoint with JWT Token and Validation of request and response of each route. It contained a documentation folder which contain swagger documentation easy for front-end developer to use and understand. It contained Pm2 which helps to restart, reload and monitor application in production, provides zero downtime availability. It has Nodemailer which will send email.
5+
This Boilerplate have a basic CRUD operation with MySQL, authetication of API endpoint with JWT Token and Validation of request and response of each route. It contained a documentation folder which contain swagger documentation easy for front-end developer to use and understand. It contained Pm2 which helps to restart, reload and monitor application in production, provides zero downtime availability. It has Nodemailer which will send email. It has bcrypt module which will encrypt password and make it more secure.
66

77
# PreRequisite
88

@@ -75,6 +75,17 @@ npm install nodemailer
7575

7676
---
7777

78+
---
79+
80+
### 8. bcrypt
81+
```
82+
npm install bcrypt
83+
```
84+
* bcrypt will encrypt your password throughing hashing so your password won't store as plain text .[know more about bcrypt](https://www.npmjs.com/package/bcrypt)
85+
* To Know more about nodemailer [check here](https://www.npmjs.com/package/bcrypt)
86+
87+
---
88+
7889
# Get Started
7990

8091
1. `$ git clone https://github.com/yug95/node-mysql.git`
@@ -118,6 +129,7 @@ Example object for login and Post request -
118129
* Includes `documents` folder which contain swagger representation both in JSON and HTML, which will help front-end developer for better understanding.
119130
* `Pm2` a process manager which help to watch, reload, restart and monitor with load balancer in each and every activity.
120131
* `nodemailer` is used to send mail over SMTP. as for i now used for sending mail if error comes.
132+
* `bcrypt` is used to encrypt your password through salt and hashing technique and which won't store password as plain text in database.
121133

122134
# Swagger Related task
123135

app/models/authentic.model.js

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,73 @@
11
var db = require('../../config/database');
2+
var dbFunc = require('../../config/db-function');
3+
const bcrypt = require('bcrypt');
24

35
var authenticModel = {
4-
authentic:authentic
6+
authentic: authentic,
7+
signup: signup
58
}
69

710
function authentic(authenticData) {
8-
return new Promise((resolve,reject) => {
9-
db.query(`SELECT * FROM user WHERE username ='${authenticData.username}' AND password='${authenticData.password}'`,(error,rows,fields)=>{
10-
if(!!error) {
11+
return new Promise((resolve, reject) => {
12+
db.query(`SELECT * FROM user WHERE username ='${authenticData.username}'`, (error, rows, fields) => {
13+
if (error) {
1114
reject(error);
1215
} else {
13-
resolve(rows);
16+
bcrypt.compare(authenticData.password, rows[0].password, function (err, isMatch) {
17+
if (err) {
18+
reject(error);
19+
} else if (isMatch) {
20+
resolve(rows);
21+
}
22+
else {
23+
reject({"success":false,"message":"password doesnot match"});
24+
}
25+
});
26+
1427
}
15-
});
16-
});
28+
});
29+
});
30+
31+
}
32+
33+
34+
function signup(user) {
35+
return new Promise((resolve, reject) => {
36+
bcrypt.genSalt(10, function (err, salt) {
37+
if (err) {
38+
return next(err);
39+
}
40+
bcrypt.hash(user.password, salt, function (err, hash) {
41+
if (err) {
42+
return next(err);
43+
}
44+
user.password = hash;
45+
db.query("SELECT * FROM user WHERE username='"+user.username+"'", (error, rows, fields) => {
46+
if (error) {
47+
dbFunc.connectionRelease;
48+
reject(error);
49+
} else if(rows.length>0) {
50+
dbFunc.connectionRelease;
51+
reject({"success":false,"message":"user already exist ! try with different user"});
52+
} else {
53+
db.query("INSERT INTO user(username,password)VALUES('" + user.username + "','" + user.password + "')", (error, rows, fields) => {
54+
if (error) {
55+
dbFunc.connectionRelease;
56+
reject(error);
57+
} else {
58+
dbFunc.connectionRelease;
59+
resolve(rows);
60+
}
61+
});
62+
}
63+
});
64+
})
65+
66+
});
67+
});
1768
}
1869

1970
module.exports = authenticModel;
2071

72+
73+

app/models/user-model.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var db = require('../../config/database');
22
var dbFunc = require('../../config/db-function');
3+
34
var userModel = {
45
getAllUser:getAllUser,
56
addUser:addUser,
@@ -10,7 +11,7 @@ var userModel = {
1011

1112
function getAllUser() {
1213
return new Promise((resolve,reject) => {
13-
db.query("SELECT * FROM user",(error,rows,fields)=>{
14+
db.query("SELECT * FROM test",(error,rows,fields)=>{
1415
if(!!error) {
1516
dbFunc.connectionRelease;
1617
reject(error);
@@ -24,7 +25,7 @@ function getAllUser() {
2425

2526
function getUserById(id) {
2627
return new Promise((resolve,reject) => {
27-
db.query("SELECT * FROM user WHERE id ="+id.id,(error,rows,fields)=>{
28+
db.query("SELECT * FROM test WHERE id ="+id.id,(error,rows,fields)=>{
2829
if(!!error) {
2930
dbFunc.connectionRelease;
3031
reject(error);
@@ -38,22 +39,22 @@ function getUserById(id) {
3839

3940
function addUser(user) {
4041
return new Promise((resolve,reject) => {
41-
db.query("INSERT INTO user(username,password)VALUES('"+user.username+"','"+user.password+"')",(error,rows,fields)=>{
42-
if(!!error) {
42+
db.query("INSERT INTO test(name,age,state,country)VALUES('"+user.name+"','"+user.age+"','"+user.state+"','"+user.country+"')",(error,rows,fields)=>{
43+
if(error) {
4344
dbFunc.connectionRelease;
4445
reject(error);
4546
} else {
4647
dbFunc.connectionRelease;
4748
resolve(rows);
4849
}
49-
});
50-
})
50+
});
51+
});
5152
}
5253

5354

5455
function updateUser(id,user) {
5556
return new Promise((resolve,reject) => {
56-
db.query("UPDATE user set username='"+user.username+"',password='"+user.password+"' WHERE id='"+id+"'",(error,rows,fields)=>{
57+
db.query("UPDATE test set name='"+user.name+"',age='"+user.age+"',state='"+user.state+"',country='"+user.country+"' WHERE id='"+id+"'",(error,rows,fields)=>{
5758
if(!!error) {
5859
dbFunc.connectionRelease;
5960
reject(error);
@@ -67,7 +68,7 @@ function updateUser(id,user) {
6768

6869
function deleteUser(id) {
6970
return new Promise((resolve,reject) => {
70-
db.query("DELETE FROM user WHERE id='"+id+"'",(error,rows,fields)=>{
71+
db.query("DELETE FROM test WHERE id='"+id+"'",(error,rows,fields)=>{
7172
if(!!error) {
7273
dbFunc.connectionRelease;
7374
reject(error);

app/routes/authentic.route.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const authenticService = require('../services/authentic.service');
2-
var schema = require('../schema/userValidationSchema.json')
2+
var schema = require('../schema/loginValidationSchema.json')
33
var iValidator = require('../../common/iValidator');
44
var errorCode = require('../../common/error-code');
55
var errorMessage = require('../../common/error-methods');
@@ -11,6 +11,8 @@ const jwt = require('jsonwebtoken');
1111
function init(router) {
1212
router.route('/login')
1313
.post(authentic);
14+
router.route('/signup')
15+
.post(signup);
1416
}
1517

1618
function authentic(req,res) {
@@ -40,6 +42,30 @@ function authentic(req,res) {
4042
}
4143

4244

45+
function signup(req,res) {
46+
var signUpData=req.body;
47+
48+
//Validating the input entity
49+
var json_format = iValidator.json_schema(schema.postSchema, signUpData, "signUpData");
50+
if (json_format.valid == false) {
51+
return res.status(422).send(json_format.errorMessage);
52+
}
53+
54+
authenticService.signup(signUpData).then((data) => {
55+
if(data) {
56+
res.json({
57+
"success":true,
58+
"data":data
59+
});
60+
}
61+
}).catch((err) => {
62+
mail.mail(err);
63+
res.json(err);
64+
});
65+
66+
}
67+
68+
4369

4470
module.exports.init = init;
4571

app/schema/loginValidationSchema.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"getSchema":{
3+
"type": "object",
4+
"required": [
5+
"id"
6+
],
7+
"properties": {
8+
"id" : {
9+
"type": "string"
10+
}
11+
}
12+
},
13+
"postSchema":{
14+
"type": "object",
15+
"required": [
16+
"username"
17+
],
18+
"properties": {
19+
"username" : {
20+
"type": "string",
21+
"minLength": 1,
22+
"maxLength": 10
23+
},
24+
"password" : {
25+
"type" : "string",
26+
"minLength": 1,
27+
"maxLength": 10
28+
}
29+
}
30+
}
31+
}

app/schema/userValidationSchema.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@
1313
"postSchema":{
1414
"type": "object",
1515
"required": [
16-
"username"
16+
"name"
1717
],
1818
"properties": {
19-
"username" : {
19+
"name" : {
2020
"type": "string",
2121
"minLength": 1,
2222
"maxLength": 10
2323
},
24-
"password" : {
25-
"type" : "string",
24+
"age" : {
25+
"type" : "integer",
26+
"minimum": 1,
27+
"maximum": 150
28+
},
29+
"state":{
30+
"type": "string",
31+
"minLength": 1,
32+
"maxLength": 10
33+
},
34+
"country": {
35+
"type": "string",
2636
"minLength": 1,
2737
"maxLength": 10
2838
}

app/services/authentic.service.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var authenticModel = require("../models/authentic.model");
22

33

44
var authenticService = {
5-
authentic: authentic
5+
authentic: authentic,
6+
signup:signup
67
}
78

89
function authentic(authenticData) {
@@ -16,6 +17,17 @@ function authentic(authenticData) {
1617

1718
}
1819

20+
function signup(signUpData) {
21+
22+
return new Promise((resolve,reject) => {
23+
authenticModel.signup(signUpData).then((data)=>{
24+
resolve(data);
25+
}).catch((err) => {
26+
reject(err);
27+
})
28+
})
29+
30+
}
1931

2032

2133

0 commit comments

Comments
 (0)