Register added
This commit is contained in:
parent
66e7e0b48d
commit
cb537f7c7f
6 changed files with 49 additions and 15 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
});
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
19
backend/service-authentication/register.js
Normal file
19
backend/service-authentication/register.js
Normal file
|
|
@ -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;
|
||||
|
|
@ -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, () => {
|
||||
|
|
|
|||
Reference in a new issue