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 @@
<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 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>
......
"use strict";
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){
......@@ -20,8 +22,14 @@ function change_path(path){
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
......@@ -47,3 +47,7 @@ button:hover {
font-size: 10pt;
text-align: center;
}
/* #menu-more {
} */
\ No newline at end of file
......@@ -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'));
......
......@@ -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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment