From cb537f7c7f686865ffca15309fa5a4788138f996 Mon Sep 17 00:00:00 2001 From: NyxiumYuuki Date: Fri, 28 May 2021 11:55:45 +0200 Subject: [PATCH] Register added --- backend/service-authentication/auth.js | 7 +++---- backend/service-authentication/checkLogin.js | 7 ++----- .../service-authentication/mongodbConnect.js | 1 - .../service-authentication/mongodbQueries.js | 18 +++++++++++++++++- backend/service-authentication/register.js | 19 +++++++++++++++++++ backend/service-authentication/server.js | 12 ++++++++---- 6 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 backend/service-authentication/register.js diff --git a/backend/service-authentication/auth.js b/backend/service-authentication/auth.js index a0a184f..172742f 100644 --- a/backend/service-authentication/auth.js +++ b/backend/service-authentication/auth.js @@ -30,11 +30,10 @@ async function authenticate(req, res) { const login = req.body.login; const password = req.body.password; - const userList = await queries.checkLoginQuery(login, password); - if (userList !== undefined && userList[0] !== undefined && userList[0].idUtilisateur > 0){ - console.log("check"); + const user = await queries.checkLoginQuery(login, password); + if (user === 1){ setSessionCookie (req, res, { username: login}); - return userList[0].idUtilisateur; + return user; } else { setSessionCookie (req, res, {username: -1}); return -1; diff --git a/backend/service-authentication/checkLogin.js b/backend/service-authentication/checkLogin.js index 3de2160..728fb28 100644 --- a/backend/service-authentication/checkLogin.js +++ b/backend/service-authentication/checkLogin.js @@ -8,11 +8,8 @@ async function checkLogin (req,res) { return sendError(res, 'Vous n\'avez pas envoyé le champ password'); const result = await auth.authenticate(req, res); - if (result > 0){ - return sendMessage(res, { - id: result, - username: req.body.login - }); + if (result === 1){ + return sendMessage(res, true); } else{ return sendError(res, 'Invalid username or password'); diff --git a/backend/service-authentication/mongodbConnect.js b/backend/service-authentication/mongodbConnect.js index 57643a1..dd1d4b9 100644 --- a/backend/service-authentication/mongodbConnect.js +++ b/backend/service-authentication/mongodbConnect.js @@ -10,7 +10,6 @@ module.exports = { console.log('mongodb-authentication-checkConnection'+client===undefined); if (client !== undefined) console.log(client.isConnected()); db = client.db(config.mongodbDatabase); - return callback( err ); }); }, diff --git a/backend/service-authentication/mongodbQueries.js b/backend/service-authentication/mongodbQueries.js index 62bb37a..d70afd6 100644 --- a/backend/service-authentication/mongodbQueries.js +++ b/backend/service-authentication/mongodbQueries.js @@ -8,9 +8,25 @@ function checkLoginQuery(login, password){ return new Promise((resolve, reject) => { resolve(mongoDB.collection(config.mongodbUtilisateurs).find( {login: login, password: password}, - {projection: {idUtilisateur: 1, _id: 0}}).toArray()); + {projection: {_id: 1}}).count()); }); } module.exports.checkLoginQuery = checkLoginQuery; +function register(login, password){ + // INSERT INTO users(login, password) + return new Promise((resolve, reject) => { + mongoDB.collection(config.mongodbUtilisateurs).insertOne( + { + login: login, + password: password + },{},function(err,res){ + console.log(res); + if(res !== undefined){ + resolve(res.insertedCount === 1); + } + }); + }); +} +module.exports.register = register; diff --git a/backend/service-authentication/register.js b/backend/service-authentication/register.js new file mode 100644 index 0000000..b263b1c --- /dev/null +++ b/backend/service-authentication/register.js @@ -0,0 +1,19 @@ +const {sendError, sendMessage} = require ("./message"); +const queries = require('./mongodbQueries'); + +async function register(req,res) { + if (typeof req.body.login === '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(login, password); + if (register){ + return sendMessage(res, null); + } + else{ + return sendError(res, 'Error registering'); + } +} +module.exports = register; diff --git a/backend/service-authentication/server.js b/backend/service-authentication/server.js index b5eb59d..65ff548 100644 --- a/backend/service-authentication/server.js +++ b/backend/service-authentication/server.js @@ -15,13 +15,17 @@ app.use(cors({origin: 'http://127.0.0.1:4200', credentials: true})); const mongoConnect = require('./mongodbConnect'); mongoConnect.connectToServer(function( err, client ) { - if (err) - console.log(err); + if (err) console.log(err); const checkLogin = require('./checkLogin'); - app.post('/checkLogin', (req, res) => { - checkLogin(req,res); + app.post('/checkLogin', (req, res) => { + checkLogin(req,res); + }); + + const register = require('./register'); + app.post('/register', (req, res) => { + register(req,res); }); app.listen(port, () => {