From 8943f25fe4acaa98169a7497b6eed7eda8ed9d8e Mon Sep 17 00:00:00 2001 From: Nicolas Paschoud <nicolas.paschoud@etu.hesge.ch> Date: Wed, 18 Dec 2019 14:28:29 +0100 Subject: [PATCH] Login / logout + create file --- projet/front/index.html | 19 +++++++--- projet/front/show-content.js | 65 ++++++++++++++++++++++++++++------- projet/front/styles/style.css | 6 +++- projet/hyperdrive-rest.js | 51 ++++++++++++--------------- projet/sql-request.js | 17 +++++++-- 5 files changed, 108 insertions(+), 50 deletions(-) diff --git a/projet/front/index.html b/projet/front/index.html index fd89ca1..402b2f7 100644 --- a/projet/front/index.html +++ b/projet/front/index.html @@ -15,13 +15,22 @@ <div id="hyperdrive"> <div id="menu"> <h3>Menu</h3> - <input type="text" id="username"> - <input type="password" id="passwd"> - <button onclick="login()">Login</button> - </div> + <div id="login-div"> + <input type="text" id="username"><br> + <input type="password" id="passwd"><br> + <button onclick="login()" id="login-button">Login</button> + </div> + <div id="menu-more" style="display: none;"> + <input type="text" id="foldername" placeholder="folder name"> + <button onclick="newFolder()">New Folder</button><br> + <input type="text" id="filename" placeholder="file name"> + <button onclick="newFile()">New File</button><br> + <button onclick="showSharedContent()">Shared with me</button> + </div> + </div> <div id="drive"> <h3>Drive</h3> - <p id="dir_name"></p> + <div id="dir_name"></div> <div id="content-drive"> </div> diff --git a/projet/front/show-content.js b/projet/front/show-content.js index cb8518c..f24b312 100644 --- a/projet/front/show-content.js +++ b/projet/front/show-content.js @@ -1,9 +1,11 @@ "use strict"; -function change_path(path){ +let my_login = null; +let token = null; + +function change_path(path) { document.getElementById("content-drive").innerHTML = ""; $.ajax({url: 'change-path'+path, success: function(result){ - console.log(result); appendLinkToParent(path, result[0].parent); for (let i in result){ if (result[i].file_id){ @@ -18,10 +20,16 @@ function change_path(path){ }}); } -function appendLinkToParent(path, parent){ +function appendLinkToParent(path, parent) { let el = document.getElementById("dir_name"); + let btn = "" + if (parent) { + btn = `<button onclick="change_path('${parent}')"><-</button>`; + } el.innerHTML = ` - Directory : ${path} + <p>Directory :</p> + <p id="path-dir-drive">${path}</p> + ${btn} `; } @@ -29,12 +37,45 @@ function appendLinkToParent(path, parent){ function login() { let username = document.getElementById("username").value let passwd = document.getElementById("passwd").value - console.log("login", - username, - passwd - ); - let log_ok = true; - if (log_ok) { - change_path('/' + username) - } + + $.ajax({url: 'login?user='+username+'&pass='+passwd, success: function(result){ + my_login = username; + token = result.signedToken; + change_path('/' + username); + let el = document.getElementById("login-button"); + el.innerHTML = "Disconnect"; + el.onclick = disconnect; + document.getElementById("menu-more").style.display = "block"; + }}); +} + +function disconnect(){ + $.ajax({url: 'logout?token=' + token, success: function(result){ + my_login = username; + token = result.signedToken; + let el = document.getElementById("login-button"); + el.innerHTML = "Login"; + el.onclick = login; + document.getElementById("content-drive").innerHTML = ""; + document.getElementById("dir_name").innerHTML = ""; + document.getElementById("menu-more").style.display = "none"; + }}); +} + +function showSharedContent() { + +} + +function newFolder() { + let foldername = document.getElementById("foldername").value; + let path = document.getElementById("path-dir-drive").textContent; + console.log('create-path' + path + "/" + foldername); + $.ajax({url: 'create-path' + path + "/" + foldername + '/' + token, success: function(result){ + console.log(result); + }}); +} + +function newFile() { + filename = document.getElementById("filename").value; + console.log(filename); } \ No newline at end of file diff --git a/projet/front/styles/style.css b/projet/front/styles/style.css index 50a5700..9619a7b 100644 --- a/projet/front/styles/style.css +++ b/projet/front/styles/style.css @@ -46,4 +46,8 @@ button:hover { .file p{ font-size: 10pt; text-align: center; -} \ No newline at end of file +} + +/* #menu-more { + +} */ \ No newline at end of file diff --git a/projet/hyperdrive-rest.js b/projet/hyperdrive-rest.js index 23e3d54..07162ba 100644 --- a/projet/hyperdrive-rest.js +++ b/projet/hyperdrive-rest.js @@ -114,8 +114,14 @@ app.get('/login', (req, res) => { const user = req.query['user']; const pass = req.query['pass']; - // userObject = sql.userExist(user, pass); + sql.userExist(user, pass, (element) => { + if (element) + check_login(user, pass, element, res); + }); + +}); +function check_login(user, pass, userObject, res) { if (!user || !pass) { res.send({ "route": "/login", @@ -124,23 +130,8 @@ app.get('/login', (req, res) => { }) } else{ - - // mock for a SQL query - users = { - "noe": { "pass_enc": "my_pass".hashCode() }, - "nicolas" : { "pass_enc": "your_pass".hashCode() } - } - /* - More like this : - { - login: "a", - passwd: "test" - } - */ - - if (user in users){ - if (users[user].pass_enc == pass.hashCode()) { - + if (userObject){ + if (userObject.passwd == pass.hashCode()) { jwt = new JWT(user, pass); res.send({ @@ -168,12 +159,8 @@ app.get('/login', (req, res) => { "comment": `Username '${ user }' don't exist.` }) } - } - - -}) - +} // resCode : [ 0: Token is valid, 1: Token is not valid, 3: Empty token ] app.get('/testmytoken', (req, res) => { @@ -257,20 +244,15 @@ app.get('/share/:file_id/:to_user', (req, res) => { console.log("user : " + user) if (req.params['to_user'] && req.params['file_id']){ - to_user = req.params['to_user']; file_id = req.params['file_id']; sql.addSharing(user, to_user, file_id).then(function (r) { res.send(r); }) - } - else{ res.send("Unable to share. Please provide a user to share with and a file_id."); } - - }) /** @@ -297,7 +279,18 @@ app.get('/change-path*', (req, res) => { }) app.get('/create-path*', (req, res) => { - res.send(`Request for a create path (${req.params['0']})`) + let c = req.params['0']; + + let tok = c.split("/").pop(); + let path = c.split("/"); + path.pop(); + path = path.join("/"); + + let name = verify_token('token'); + sql.createPath(path, name, (resp, msg) => { + console.log(resp, msg); + res.send(resp + " : " + msg); + }); }) app.use(express.static('front')); diff --git a/projet/sql-request.js b/projet/sql-request.js index c2d295f..5bbb29f 100644 --- a/projet/sql-request.js +++ b/projet/sql-request.js @@ -19,10 +19,10 @@ function userExist(login, pass, callback){ con.query(q, function (err, result) { if (err) return false; if (result.length > 0) { - console.log("user already exists"); + console.log("user exists"); return callback({ login: result[0]['login'], - passwd: result[0]['passwd'] + passwd: result[0]['passwd'].hashCode() }); } else { console.log("user don't exists"); @@ -165,8 +165,18 @@ async function addSharing(login, to_user, file_id){ else{ return "Unable to share, this is not your file."; } - +} + +function createPath(path, user, callback) { + let parent = path.split("/"); + parent.pop(); + parent = parent.join("/"); + q = `INSERT INTO Paths VALUES ('${path}', '${user}', ${parent})`; + con.query(q, (err, resp) => { + if (err) return callback(false, err); + return callback(true, resp); + }); } exports.userExist = userExist; @@ -174,3 +184,4 @@ exports.addUser = addUser; exports.addPath = addPath; exports.addSharing = addSharing; exports.changeDirectory = changeDirectory; +exports.createPath = createPath; -- GitLab