updated
This commit is contained in:
parent
67a37d16ec
commit
c5406006da
8 changed files with 74 additions and 10 deletions
22
backend/service-authentication/changePassword.js
Normal file
22
backend/service-authentication/changePassword.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
const {sendError, sendMessage} = require ("./message");
|
||||
const queries = require('./mongodbQueries');
|
||||
|
||||
async function changePassword (req,res) {
|
||||
if (typeof req.body.username === 'undefined')
|
||||
return sendError(res, 'Vous n\'avez pas envoyé le champ username');
|
||||
|
||||
if (typeof req.body.password === 'undefined')
|
||||
return sendError(res, 'Vous n\'avez pas envoyé le champ password');
|
||||
|
||||
if (typeof req.body.newpassword === 'undefined')
|
||||
return sendError(res, 'Vous n\'avez pas envoyé le champ newpassword');
|
||||
|
||||
const change = await queries.changePasswordQuery(req.body.username, req.body.password, req.body.newpassword);
|
||||
if (change){
|
||||
return sendMessage(res, change);
|
||||
}
|
||||
else{
|
||||
return sendError(res, 'cant change');
|
||||
}
|
||||
}
|
||||
module.exports = changePassword;
|
||||
|
|
@ -42,3 +42,17 @@ function getUsersQuery(username){
|
|||
}
|
||||
module.exports.getUsersQuery = getUsersQuery
|
||||
|
||||
function changePasswordQuery(login, password, newPassword){
|
||||
return new Promise((resolve, reject) => {
|
||||
mongoDB.collection(config.mongodbUtilisateurs).findOneAndUpdate(
|
||||
{'login': login, 'password': password},
|
||||
{$set: { 'login': login, 'password': newPassword}}
|
||||
,function(err,res){
|
||||
if(res !== undefined){
|
||||
console.log(res);
|
||||
resolve(res.lastErrorObject.n === 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
module.exports.changePasswordQuery = changePasswordQuery;
|
||||
|
|
|
|||
|
|
@ -2,19 +2,19 @@ const {sendError, sendMessage} = require ("./message");
|
|||
const queries = require('./mongodbQueries');
|
||||
|
||||
async function register(req,res) {
|
||||
if (typeof req.body.login === 'undefined')
|
||||
if (typeof req.body.username === 'undefined')
|
||||
return sendError(res, 'Vous n\'avez pas envoyé le champ login');
|
||||
if (typeof req.body.password === 'undefined')
|
||||
return sendError(res, 'Vous n\'avez pas envoyé le champ password');
|
||||
|
||||
|
||||
const register = await queries.register(req.body.login, req.body.password);
|
||||
const register = await queries.register(req.body.username, req.body.password);
|
||||
if (register){
|
||||
console.log('Register : '+req.body.login);
|
||||
console.log('Register : '+req.body.username);
|
||||
return sendMessage(res, null);
|
||||
}
|
||||
else{
|
||||
return sendError(res, 'Error registering');
|
||||
return sendError(res, 'Username already taken');
|
||||
}
|
||||
}
|
||||
module.exports = register;
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ mongoConnect.connectToServer(function( err, client ) {
|
|||
if (err) console.log(err);
|
||||
const checkLogin = require('./checkLogin');
|
||||
const register = require('./register');
|
||||
const changePassword = require('./changePassword');
|
||||
const getUsers = require('./getUsers');
|
||||
const queries = require('./mongodbQueries');
|
||||
const auth = require('./auth');
|
||||
|
|
@ -46,6 +47,10 @@ mongoConnect.connectToServer(function( err, client ) {
|
|||
getUsers(req,res);
|
||||
});
|
||||
|
||||
app.post('/changePassword', (req, res) => {
|
||||
changePassword(req,res);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log (`listening on port ${port}`);
|
||||
});
|
||||
|
|
|
|||
Reference in a new issue