Correct the login route.
This commit is contained in:
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@@ -5,13 +5,15 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
|
"name": "Launch Program with debugger",
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Launch Program",
|
"cwd": "${workspaceFolder}",
|
||||||
"skipFiles": [
|
"runtimeExecutable": "npm",
|
||||||
"<node_internals>/**"
|
"runtimeArgs": [
|
||||||
|
"start"
|
||||||
],
|
],
|
||||||
"program": "${workspaceFolder}/src/js/app.js"
|
"port": 5858
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -3,14 +3,4 @@ const yaml = require('yaml');
|
|||||||
|
|
||||||
const configurationFile = fs.readFileSync('src/resources/application.yml', 'utf8');
|
const configurationFile = fs.readFileSync('src/resources/application.yml', 'utf8');
|
||||||
const configuration = yaml.parse(configurationFile);
|
const configuration = yaml.parse(configurationFile);
|
||||||
console.log(configuration);
|
|
||||||
module.exports = configuration;
|
module.exports = configuration;
|
||||||
|
|
||||||
// module.exports = {
|
|
||||||
// database: {
|
|
||||||
// url: 'mongodb://localhost:27017',
|
|
||||||
// username: 'express-user',
|
|
||||||
// password: 'P@ssword1',
|
|
||||||
// database: 'express-test'
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
@@ -2,15 +2,16 @@ const router = require('express').Router();
|
|||||||
const tokenService = require('../service/tokenService');
|
const tokenService = require('../service/tokenService');
|
||||||
const userService = require('../service/userService');
|
const userService = require('../service/userService');
|
||||||
|
|
||||||
// Develop routes here
|
|
||||||
router.post('/login', (request, response) => {
|
router.post('/login', (request, response) => {
|
||||||
const loginRequest = request.body;
|
const loginRequest = request.body;
|
||||||
|
|
||||||
if (!!loginRequest && userService.areCredentialsValid(loginRequest.login, loginRequest.password)) {
|
if (!loginRequest) {
|
||||||
const tokenPayload = { login: loginRequest.login };
|
|
||||||
response.json(tokenService.build(tokenPayload));
|
|
||||||
} else {
|
|
||||||
response.status(403).send();
|
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());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,15 @@ class MongoClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
find(collectionName, query, callback) {
|
find(collectionName, query, onSuccess, onError) {
|
||||||
this.db.collection(collectionName).find(query).toArray()
|
this.db.collection(collectionName).find(query).toArray()
|
||||||
.then(results => {
|
.then(results => {
|
||||||
console.log(`Entities ${collectionName} founded.`);
|
console.log(`Entities ${collectionName} founded.`);
|
||||||
callback(results);
|
onSuccess(results);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ module.exports = class Repository {
|
|||||||
/**
|
/**
|
||||||
* Returns the entities that matches criteria in {@code query}.
|
* Returns the entities that matches criteria in {@code query}.
|
||||||
* @param {*} query The query which contains criteria to find some entities.
|
* @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);
|
convertIdToMongodbFormat(query);
|
||||||
this.mongoClient.find(this.collectionName, query, callback);
|
this.mongoClient.find(this.collectionName, query, onSuccess, onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,21 +4,44 @@ const passwordService = require('./passwordService');
|
|||||||
const userRepository = new Repository('users');
|
const userRepository = new Repository('users');
|
||||||
|
|
||||||
class UserService {
|
class UserService {
|
||||||
getUser(login) {
|
/**
|
||||||
return login === 'toto'
|
* Get a user from database by its login.
|
||||||
? {login: 'toto', password: passwordService.hashPassword('pwd')}
|
* @param {String} login User login.
|
||||||
: undefined;
|
* @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() {
|
isAuthenticated() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
areCredentialsValid(login, password) {
|
/**
|
||||||
const user = this.getUser(login);
|
*
|
||||||
// If login is incorrect, the "getUser" function will return "undefined".
|
* @param {String} login User login.
|
||||||
// So if "user" is not "undefined", this proofs that login is correct.
|
* @param {String} password User password, in plain text.
|
||||||
return !!user && passwordService.areSamePasswords(password, user.password);
|
* @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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user