diff --git a/.vscode/launch.json b/.vscode/launch.json index 61b1623..073c8b8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,13 +5,15 @@ "version": "0.2.0", "configurations": [ { + "name": "Launch Program with debugger", "type": "node", "request": "launch", - "name": "Launch Program", - "skipFiles": [ - "/**" + "cwd": "${workspaceFolder}", + "runtimeExecutable": "npm", + "runtimeArgs": [ + "start" ], - "program": "${workspaceFolder}/src/js/app.js" + "port": 5858 } ] } \ No newline at end of file diff --git a/src/js/configuration.js b/src/js/configuration.js index b7f3720..ab6666c 100644 --- a/src/js/configuration.js +++ b/src/js/configuration.js @@ -3,14 +3,4 @@ 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' -// } -// }; \ No newline at end of file +module.exports = configuration; \ No newline at end of file diff --git a/src/js/controller/userCtrl.js b/src/js/controller/userCtrl.js index 6bc57d0..c5cd1c7 100644 --- a/src/js/controller/userCtrl.js +++ b/src/js/controller/userCtrl.js @@ -2,15 +2,16 @@ const router = require('express').Router(); const tokenService = require('../service/tokenService'); const userService = require('../service/userService'); -// Develop routes here router.post('/login', (request, response) => { const loginRequest = request.body; - if (!!loginRequest && userService.areCredentialsValid(loginRequest.login, loginRequest.password)) { - const tokenPayload = { login: loginRequest.login }; - response.json(tokenService.build(tokenPayload)); - } else { + if (!loginRequest) { response.status(403).send(); + } else { + userService.checkCredentials(loginRequest.login, loginRequest.password, () => { + const tokenPayload = { login: loginRequest.login }; + response.json(tokenService.build(tokenPayload)); + }, () => response.status(403).send()); } }); diff --git a/src/js/repository/mongoClient.js b/src/js/repository/mongoClient.js index 40ed8f5..2024f2e 100644 --- a/src/js/repository/mongoClient.js +++ b/src/js/repository/mongoClient.js @@ -14,14 +14,15 @@ class MongoClient { }); } - find(collectionName, query, callback) { + find(collectionName, query, onSuccess, onError) { this.db.collection(collectionName).find(query).toArray() .then(results => { console.log(`Entities ${collectionName} founded.`); - callback(results); + onSuccess(results); }) .catch(error => { - throw new Error(`Unable to find entities in collection ${collectionName}: ${error}`); + console.error(`Unable to find entities in collection ${collectionName}: ${error}`); + onError(error); }); } diff --git a/src/js/repository/repository.js b/src/js/repository/repository.js index f17bf90..44de763 100644 --- a/src/js/repository/repository.js +++ b/src/js/repository/repository.js @@ -23,11 +23,11 @@ module.exports = class Repository { /** * Returns the entities that matches criteria in {@code query}. * @param {*} query The query which contains criteria to find some entities. - * @param {*} callback The function to execute after getting entities. + * @param {*} onSuccess The function to execute after getting entities. */ - find(query, callback) { + find(query, onSuccess, onError) { convertIdToMongodbFormat(query); - this.mongoClient.find(this.collectionName, query, callback); + this.mongoClient.find(this.collectionName, query, onSuccess, onError); } /** diff --git a/src/js/service/userService.js b/src/js/service/userService.js index 6a04100..d8cfdb6 100644 --- a/src/js/service/userService.js +++ b/src/js/service/userService.js @@ -4,21 +4,44 @@ const passwordService = require('./passwordService'); const userRepository = new Repository('users'); class UserService { - getUser(login) { - return login === 'toto' - ? {login: 'toto', password: passwordService.hashPassword('pwd')} - : undefined; + /** + * Get a user from database by its login. + * @param {String} login User login. + * @param {Function} onSuccess Callback function to execute if a user exists with this login. + * @param {Function} onError Callback function to execute if not any user exists with this login. + */ + getUser(login, onSuccess, onError) { + userRepository.find({login: login}, results => onSuccess(results[0]), onError); } + /** + * + */ 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); + /** + * + * @param {String} login User login. + * @param {String} password User password, in plain text. + * @param {Function} onSuccess Callback function to execute if a user exists with this login. + * @param {Function} onError Callback function to execute if not any user exists with this login. + */ + checkCredentials(login, password, onSuccess, onError) { + this.getUser( + login, + dbUser => { + if (!!dbUser && passwordService.areSamePasswords(password, dbUser.password)) { + onSuccess(); + } else { + onError(); + } + }, + // If login is incorrect, the "getUser" function will return "undefined". + // So if "user" is "undefined", this proofs that login is incorrect + onError + ); } }