Skip to content
Snippets Groups Projects
Commit 925b11a8 authored by nicolas.paschoud's avatar nicolas.paschoud
Browse files

Update the login check

parent 8944a022
Branches main
No related tags found
No related merge requests found
......@@ -253,6 +253,8 @@ app.get('/download/:file_id', (req, res) => {
* param
*/
app.get('/change-path*', (req, res) => {
sql.changeDirectory('a', req.params['0']);
res.send(`Request for a change path (${req.params['0']})`)
})
......
......@@ -14,22 +14,21 @@ var con = mysql.createConnection({
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
function userExist(login, pass){
function userExist(login, pass, callback){
const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`;
return new Promise (() => {
con.query(q, function (err, result) {
if (err) return false;
if (result.length > 0) {
console.log("user exists");
return {
console.log("user already exists");
return callback({
login: result[0]['login'],
passwd: result[0]['passwd']
};
});
} else {
console.log("user don't exists");
return callback(false);
}
});
});
}
/**
......@@ -37,16 +36,17 @@ function userExist(login, pass){
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
async function insertUser(login, passwd) {
function insertUser(login, passwd, callback){
let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`;
return new Promise( () => {
userExist(login, passwd).then(
userExist(login, passwd, (userExist) => {
if (!userExist){
con.query(q, (err, res) => {
if (err) return false;
console.log("User", login, "inserted in the db")
return true;
})
)
if (err) return callback(false);
if (!res) {console.log(res);return callback(false);}
console.log("User", login, "inserted in the db");
return callback(true);
});
}
});
}
......@@ -56,18 +56,16 @@ async function insertUser(login, passwd) {
* @param {string} login User's path
* @param {string} parent Parent of the new folder
*/
function addPath(path, login, parent){
function addPath(path, login, parent, callback){
let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`;
return new Promise(() => {
con.query(q, function(err, res) {
if (err) {
console.log("Error while adding a new path");
console.log(err);
return false;
return callback(false);
}
console.log("New path", path, "added succesfully !");
return true;
});
return callback(true);
});
}
......@@ -77,13 +75,18 @@ function addPath(path, login, parent){
* @param {string} login This is the login of the user
* @param {string} pass This is the password of the user
*/
async function addUser(login, passwd){
return new Promise(async () => {
await insertUser(login, passwd).then(
addPath('/'+login, login, null));
function addUser(login, passwd) {
insertUser(login, passwd, (insertOk) => {
console.log(insertOk);
if (insertOk) {
addPath('/'+login, login, null, (res)=>{});
console.log("Add ok");
}
});
}
/**
+----------------+-------+-----------+---------+-----------+
| paths | login | parent | file_id | file_name |
......@@ -93,20 +96,33 @@ async function addUser(login, passwd){
| /a/coucou/test | a | /a/coucou | NULL | NULL |
+----------------+-------+-----------+---------+-----------+
*/
async function changePath(login, path){
async function changeDirectory(login, path, callback){
q = `
SELECT Paths.paths, login, parent, Files.file_id, Files.file_name
FROM Paths
LEFT JOIN Files ON Files.paths = Paths.paths
WHERE Files.file_id IS NOT NULL
AND login='${login}' AND Paths.paths='${path}'`;
WHERE Paths.login='${login}'
AND Paths.paths='${path}'
OR Paths.parent='${path}';`;
con.query(q, (err, res) => {
if (err) {
console.log("Error while loading the path");
return false;
return callback(false, err);
}
let content = [];
for (let i in res){
content.push({
paths: res[i].paths,
login: res[i].login,
parent: res[i].parent,
file_id: res[i].file_id,
file_name: res[i].file_name
});
}
console.log(res);
console.log(content);
});
return [
......@@ -134,6 +150,6 @@ async function changePath(login, path){
];
}
exports.userExist = userExist;
exports.addUser = addUser;
exports.addPath = addPath;
exports.changeDirectory = changeDirectory;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment