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

Login / logout + create file

parent 994aad4a
No related branches found
No related tags found
No related merge requests found
...@@ -15,13 +15,22 @@ ...@@ -15,13 +15,22 @@
<div id="hyperdrive"> <div id="hyperdrive">
<div id="menu"> <div id="menu">
<h3>Menu</h3> <h3>Menu</h3>
<input type="text" id="username"> <div id="login-div">
<input type="password" id="passwd"> <input type="text" id="username"><br>
<button onclick="login()">Login</button> <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>
<div id="drive"> <div id="drive">
<h3>Drive</h3> <h3>Drive</h3>
<p id="dir_name"></p> <div id="dir_name"></div>
<div id="content-drive"> <div id="content-drive">
</div> </div>
......
"use strict"; "use strict";
let my_login = null;
let token = null;
function change_path(path) { function change_path(path) {
document.getElementById("content-drive").innerHTML = ""; document.getElementById("content-drive").innerHTML = "";
$.ajax({url: 'change-path'+path, success: function(result){ $.ajax({url: 'change-path'+path, success: function(result){
console.log(result);
appendLinkToParent(path, result[0].parent); appendLinkToParent(path, result[0].parent);
for (let i in result){ for (let i in result){
if (result[i].file_id){ if (result[i].file_id){
...@@ -20,8 +22,14 @@ function change_path(path){ ...@@ -20,8 +22,14 @@ function change_path(path){
function appendLinkToParent(path, parent) { function appendLinkToParent(path, parent) {
let el = document.getElementById("dir_name"); let el = document.getElementById("dir_name");
let btn = ""
if (parent) {
btn = `<button onclick="change_path('${parent}')"><-</button>`;
}
el.innerHTML = ` el.innerHTML = `
Directory : ${path} <p>Directory :</p>
<p id="path-dir-drive">${path}</p>
${btn}
`; `;
} }
...@@ -29,12 +37,45 @@ function appendLinkToParent(path, parent){ ...@@ -29,12 +37,45 @@ function appendLinkToParent(path, parent){
function login() { function login() {
let username = document.getElementById("username").value let username = document.getElementById("username").value
let passwd = document.getElementById("passwd").value let passwd = document.getElementById("passwd").value
console.log("login",
username, $.ajax({url: 'login?user='+username+'&pass='+passwd, success: function(result){
passwd my_login = username;
); token = result.signedToken;
let log_ok = true; change_path('/' + username);
if (log_ok) { let el = document.getElementById("login-button");
change_path('/' + username) 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
...@@ -47,3 +47,7 @@ button:hover { ...@@ -47,3 +47,7 @@ button:hover {
font-size: 10pt; font-size: 10pt;
text-align: center; text-align: center;
} }
/* #menu-more {
} */
\ No newline at end of file
...@@ -114,8 +114,14 @@ app.get('/login', (req, res) => { ...@@ -114,8 +114,14 @@ app.get('/login', (req, res) => {
const user = req.query['user']; const user = req.query['user'];
const pass = req.query['pass']; 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) { if (!user || !pass) {
res.send({ res.send({
"route": "/login", "route": "/login",
...@@ -124,23 +130,8 @@ app.get('/login', (req, res) => { ...@@ -124,23 +130,8 @@ app.get('/login', (req, res) => {
}) })
} }
else{ else{
if (userObject){
// mock for a SQL query if (userObject.passwd == pass.hashCode()) {
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()) {
jwt = new JWT(user, pass); jwt = new JWT(user, pass);
res.send({ res.send({
...@@ -168,12 +159,8 @@ app.get('/login', (req, res) => { ...@@ -168,12 +159,8 @@ app.get('/login', (req, res) => {
"comment": `Username '${ user }' don't exist.` "comment": `Username '${ user }' don't exist.`
}) })
} }
} }
}
})
// resCode : [ 0: Token is valid, 1: Token is not valid, 3: Empty token ] // resCode : [ 0: Token is valid, 1: Token is not valid, 3: Empty token ]
app.get('/testmytoken', (req, res) => { app.get('/testmytoken', (req, res) => {
...@@ -257,20 +244,15 @@ app.get('/share/:file_id/:to_user', (req, res) => { ...@@ -257,20 +244,15 @@ app.get('/share/:file_id/:to_user', (req, res) => {
console.log("user : " + user) console.log("user : " + user)
if (req.params['to_user'] && req.params['file_id']){ if (req.params['to_user'] && req.params['file_id']){
to_user = req.params['to_user']; to_user = req.params['to_user'];
file_id = req.params['file_id']; file_id = req.params['file_id'];
sql.addSharing(user, to_user, file_id).then(function (r) { sql.addSharing(user, to_user, file_id).then(function (r) {
res.send(r); res.send(r);
}) })
} }
else{ else{
res.send("Unable to share. Please provide a user to share with and a file_id."); 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) => { ...@@ -297,7 +279,18 @@ app.get('/change-path*', (req, res) => {
}) })
app.get('/create-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')); app.use(express.static('front'));
......
...@@ -19,10 +19,10 @@ function userExist(login, pass, callback){ ...@@ -19,10 +19,10 @@ function userExist(login, pass, callback){
con.query(q, function (err, result) { con.query(q, function (err, result) {
if (err) return false; if (err) return false;
if (result.length > 0) { if (result.length > 0) {
console.log("user already exists"); console.log("user exists");
return callback({ return callback({
login: result[0]['login'], login: result[0]['login'],
passwd: result[0]['passwd'] passwd: result[0]['passwd'].hashCode()
}); });
} else { } else {
console.log("user don't exists"); console.log("user don't exists");
...@@ -165,8 +165,18 @@ async function addSharing(login, to_user, file_id){ ...@@ -165,8 +165,18 @@ async function addSharing(login, to_user, file_id){
else{ else{
return "Unable to share, this is not your file."; 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; exports.userExist = userExist;
...@@ -174,3 +184,4 @@ exports.addUser = addUser; ...@@ -174,3 +184,4 @@ exports.addUser = addUser;
exports.addPath = addPath; exports.addPath = addPath;
exports.addSharing = addSharing; exports.addSharing = addSharing;
exports.changeDirectory = changeDirectory; exports.changeDirectory = changeDirectory;
exports.createPath = createPath;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment