Refactor code and include yaml file reading.
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
"bcrypt": "^5.0.0",
|
||||
"express": "^4.17.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongodb": "^3.6.1"
|
||||
"mongodb": "^3.6.1",
|
||||
"yaml": "^1.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^7.4.0",
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
module.exports = {
|
||||
database: {
|
||||
url: 'mongodb://localhost:27017',
|
||||
username: 'express-user',
|
||||
password: 'P@ssword1',
|
||||
database: 'express-test'
|
||||
}
|
||||
};
|
||||
const fs = require('fs');
|
||||
const yaml = require('yaml');
|
||||
|
||||
const configurationFile = fs.readFileSync('src/resources/application.yml', 'utf8');
|
||||
const configuration = yaml.parse(configurationFile);
|
||||
console.log(configuration);
|
||||
module.exports = configuration;
|
||||
|
||||
// module.exports = {
|
||||
// database: {
|
||||
// url: 'mongodb://localhost:27017',
|
||||
// username: 'express-user',
|
||||
// password: 'P@ssword1',
|
||||
// database: 'express-test'
|
||||
// }
|
||||
// };
|
||||
@@ -1,18 +1,14 @@
|
||||
const router = require('express').Router();
|
||||
const Jwt = require('../jwtService');
|
||||
const tokenService = require('../service/tokenService');
|
||||
const userService = require('../service/userService');
|
||||
const passwordService = require('../service/passwordService');
|
||||
|
||||
// Develop routes here
|
||||
router.post('/login', (request, response) => {
|
||||
const loginRequest = request.body;
|
||||
|
||||
// If login is incorrect, the "getUser" function will return "undefined".
|
||||
// So if "user" is not "undefined", this proofs that login is correct.
|
||||
const user = userService.getUser(loginRequest.login);
|
||||
if (!!loginRequest && !!user && passwordService.areSamePasswords(loginRequest.password, user.password)) {
|
||||
if (!!loginRequest && userService.areCredentialsValid(loginRequest.login, loginRequest.password)) {
|
||||
const tokenPayload = { login: loginRequest.login };
|
||||
response.json(Jwt.buildToken(tokenPayload));
|
||||
response.json(tokenService.build(tokenPayload));
|
||||
} else {
|
||||
response.status(403).send();
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
class Jwt {
|
||||
buildToken(tokenPayload) {
|
||||
return jwt.sign(tokenPayload, 'secret', {expiresIn: '1h'});
|
||||
}
|
||||
|
||||
isTokenValid(token) {
|
||||
try {
|
||||
jwt.verify(token, 'secret');
|
||||
} catch (exception) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const jwtInstance = new Jwt();
|
||||
module.exports = jwtInstance;
|
||||
@@ -1,6 +1,6 @@
|
||||
const mongodb = require('mongodb');
|
||||
const configuration = require('../configuration');
|
||||
const mongoConfig = configuration.database;
|
||||
const mongoConfig = configuration.mongodb;
|
||||
|
||||
class MongoClient {
|
||||
constructor() {
|
||||
|
||||
21
src/js/service/tokenService.js
Normal file
21
src/js/service/tokenService.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const configuration = require('../configuration');
|
||||
const securityConfig = configuration.security;
|
||||
|
||||
class TokenService {
|
||||
build(tokenPayload) {
|
||||
return jwt.sign(tokenPayload, securityConfig.jwt.secret, {expiresIn: securityConfig.jwt.validity});
|
||||
}
|
||||
|
||||
isValid(token) {
|
||||
try {
|
||||
jwt.verify(token, securityConfig.jwt.secret);
|
||||
} catch (exception) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const jwtInstance = new TokenService();
|
||||
module.exports = jwtInstance;
|
||||
@@ -5,7 +5,7 @@ const userRepository = new Repository('users');
|
||||
|
||||
class UserService {
|
||||
getUser(login) {
|
||||
return login === 'toto'
|
||||
return login === 'toto'
|
||||
? {login: 'toto', password: passwordService.hashPassword('pwd')}
|
||||
: undefined;
|
||||
}
|
||||
@@ -13,6 +13,13 @@ class UserService {
|
||||
isAuthenticated() {
|
||||
return false;
|
||||
}
|
||||
|
||||
areCredentialsValid(login, password) {
|
||||
const user = this.getUser(login);
|
||||
// If login is incorrect, the "getUser" function will return "undefined".
|
||||
// So if "user" is not "undefined", this proofs that login is correct.
|
||||
return !!user && passwordService.areSamePasswords(password, user.password);
|
||||
}
|
||||
}
|
||||
|
||||
const singleton = new UserService();
|
||||
|
||||
9
src/resources/application.yml
Normal file
9
src/resources/application.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
mongodb:
|
||||
url: 'mongodb://localhost:27017'
|
||||
username: 'express-user'
|
||||
password: 'P@ssword1'
|
||||
database: 'express-test'
|
||||
security:
|
||||
jwt:
|
||||
secret: 5ubtcCCo7hWBqjNGtzzVKnLT1KxN9uS4D6kRZowCunZAYPmxtKy6mvgoxANe4WqLVfiVI7AZSVqZCtvlSWFwIsnXGH6lxeKG0U8Wu7Kw0jwfFOGLvlO8bXaB
|
||||
validity: 1h
|
||||
Reference in New Issue
Block a user