Skip to content

Commit 3018266

Browse files
author
daniel.pires
committed
Merge branch 'master' of github.com:danizavtz/tokenAuth
2 parents 8122e58 + cac952e commit 3018266

File tree

11 files changed

+63
-41
lines changed

11 files changed

+63
-41
lines changed

.eslintrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
"env": {
3-
"es6": true,
3+
"es2018": true,
44
"node": true
55
},
66
"extends": "eslint:recommended",
@@ -23,6 +23,7 @@ module.exports = {
2323
"semi": [
2424
"error",
2525
"always"
26-
]
26+
],
27+
"no-useless-catch": "off"
2728
}
2829
};

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('dotenv').config()
1+
require('dotenv').config();
22
const express = require('express');
33
const logger = require('morgan');
44
const cors = require('cors');

bin/www

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ try {
55
app.listen(process.env.PORT);
66
console.log('Microsservice login listening at http://localhost:%s', process.env.PORT);
77
} catch (e) {
8-
throw e
8+
throw e;
99
}

lib/postgres.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { Pool } = require('pg')
2-
const pool = new Pool()
1+
const { Pool } = require('pg');
2+
const pool = new Pool();
33
module.exports = {
4-
query: (text, params, callback) => {
5-
return pool.query(text, params, callback)
6-
},
7-
}
4+
query: (text, params, callback) => {
5+
return pool.query(text, params, callback);
6+
},
7+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "nodemon ./bin/www",
88
"start": "node ./bin/www",
9-
"lint": "./node_modules/.bin/eslint ./server/**/*.js",
9+
"lint": "./node_modules/.bin/eslint ./server/**/*.js ./bin/www ./lib/*.js app.js",
1010
"test": "mocha --reporter progress test/ --exit",
1111
"coverage": "nyc mocha --exit --reporter progress ./test/",
1212
"coverage:report": "nyc report --reporter=lcov --reporter=text",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
exports.secure_route = (req, res) => {
3+
req.sucesso = { msg: 'server up and running secure route'};
4+
res.status(200).json(req.sucesso);
5+
};
6+
7+
exports.index = (req, res) => {
8+
res.status(200).json({msg: 'server up and running'});
9+
};
10+
11+
exports.fallback = (req, res) => {
12+
res.status(404).json({ errors: [{location: req.path, msg: 'Not found', param: null}]});
13+
};
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
const postgres = require('../../lib/postgres');
21
const jwt = require('jsonwebtoken');
32
const crypto = require('crypto');
4-
5-
exports.logEmployee = (req, res) => {
6-
res.status(200).json({ token: 'Bearer ' + jwt.sign(req.employee, process.env.SECRET, { expiresIn: 1800 }) });//expires in 1800 seconds
7-
res.end();
8-
};
3+
const loginService = require('../services/login.service');
94

105
exports.hashPassword = (req, res, next) => {
116
crypto.scrypt(req.body.password.toString(), 'salt', 256, (err, derivedKey) => {
@@ -17,16 +12,21 @@ exports.hashPassword = (req, res, next) => {
1712
});
1813
};
1914

20-
exports.lookupLogin = (req, res, next) => {
21-
const sql = 'SELECT e.employee_id, e.login FROM employee e WHERE e.login=$1 AND e.password = $2';
22-
postgres.query(sql, [req.body.login, req.body.kdfResult], (err, result) => {
23-
if (err) {
24-
return res.status(500).json({ errors: [{ location: req.path, msg: 'Could not do login', param: req.params.id }] });
25-
}
26-
if (result.rows.length === 0) {
27-
return res.status(404).json({ errors: [{ location: req.path, msg: 'User or password does not match', param: req.params.id }] });
15+
exports.lookupLogin = async (req, res, next) => {
16+
try {
17+
const result = await loginService.lookupLogin(req.body);
18+
if (result.length === 0) {
19+
res.status(404).json({ errors: [{ location: req.path, msg: 'User or password does not match', param: req.params.id }] });
20+
return;
2821
}
29-
req.employee = result.rows[0];
22+
req.employee = result[0];
3023
next();
31-
});
32-
};
24+
} catch (err) {
25+
res.status(500).json({ errors: [{ location: req.path, msg: 'Could not do login', param: null }] });
26+
}
27+
};
28+
29+
exports.logEmployee = (req, res) => {
30+
res.status(200).json({ token: 'Bearer ' + jwt.sign(req.employee, process.env.SECRET, { expiresIn: 1800 }) });//expires in 1800 seconds
31+
res.end();
32+
};

server/index.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
const router = require('express').Router();
22

33
router.use(require('./routes/login.route'));
4-
router.get('/secure', (req, res) => {
5-
req.sucesso = { msg: 'server up and running secure route'};
6-
res.status(200).json(req.sucesso);
7-
});
8-
9-
router.get('/', (req, res) => {
10-
res.status(200).json({msg: "server up and running"});
11-
});
12-
//após tentar casar todas as rotas a ultima rota que sobrou é not found
13-
router.get('*', (req, res) => {
14-
res.status(404).json({ errors: [{location: req.path, msg: 'Not found', param: null}]});
15-
});
4+
router.use(require('./routes/index.route'));
165

176
module.exports = router;

server/routes/index.route.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const router = require('express').Router();
2+
const indexController = require('../controllers/index.controller');
3+
4+
router.get('/secure', indexController.secure_route);
5+
router.get('/', indexController.index);
6+
router.get('*', indexController.fallback);
7+
8+
module.exports = router;

server/services/login.service.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const postgres = require('../../lib/postgres');
2+
3+
exports.lookupLogin = async (usuario) => {
4+
try {
5+
const sql = 'SELECT e.employee_id, e.login FROM employee e WHERE e.login=$1 AND e.password = $2';
6+
const { rows } = await postgres.query(sql, [usuario.login, usuario.kdfResult]);
7+
return rows;
8+
} catch (err) {
9+
throw err;
10+
}
11+
};

test/spec1.login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('#Login', () => {
111111
routine: 'parserOpenTable'
112112
};
113113

114-
const stub = sinon.stub(pg, 'query').yields(new Error(mocked_error_from_database));
114+
const stub = sinon.stub(pg, 'query').rejects(mocked_error_from_database);
115115
api.post('/login/')
116116
.send({
117117
"login": "admin",

0 commit comments

Comments
 (0)