Correct the login route.

This commit is contained in:
2020-09-26 17:30:01 +02:00
parent f51079eeb4
commit 54495c0689
6 changed files with 52 additions and 35 deletions

10
.vscode/launch.json vendored
View File

@@ -5,13 +5,15 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program with debugger",
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"start"
],
"program": "${workspaceFolder}/src/js/app.js"
"port": 5858
}
]
}

View File

@@ -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'
// }
// };

View File

@@ -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)) {
if (!loginRequest) {
response.status(403).send();
} else {
userService.checkCredentials(loginRequest.login, loginRequest.password, () => {
const tokenPayload = { login: loginRequest.login };
response.json(tokenService.build(tokenPayload));
} else {
response.status(403).send();
}, () => response.status(403).send());
}
});

View File

@@ -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);
});
}

View File

@@ -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);
}
/**

View File

@@ -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);
/**
*
* @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 not "undefined", this proofs that login is correct.
return !!user && passwordService.areSamePasswords(password, user.password);
// So if "user" is "undefined", this proofs that login is incorrect
onError
);
}
}