|
@@ -6,11 +6,17 @@ const ERR_REQUEST = 1;
|
|
const ERR_AUTH = 2;
|
|
const ERR_AUTH = 2;
|
|
const ERR_UNKNOW = 3;
|
|
const ERR_UNKNOW = 3;
|
|
const ERR_TOKEN = 4;
|
|
const ERR_TOKEN = 4;
|
|
|
|
+const ERR_SERV = 5;
|
|
|
|
|
|
// Fonctions de traitement pour les routes
|
|
// Fonctions de traitement pour les routes
|
|
-function verify(req, res, next) {
|
|
|
|
- if(auth.isActivated() && auth.verify(req.body.user, req.body.token)) {
|
|
|
|
- res.json(error(ERR_TOKEN))
|
|
|
|
|
|
+function verifyAuth(req, res, next) {
|
|
|
|
+ if(req.body.user === undefined || req.body.token === undefined) {
|
|
|
|
+ res.json(error(ERR_REQUEST));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(auth.isActivated() && !auth.verify(req.body.user, req.body.token)) {
|
|
|
|
+ res.json(error(ERR_TOKEN));
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
next();
|
|
next();
|
|
}
|
|
}
|
|
@@ -34,6 +40,9 @@ function error(code) {
|
|
case ERR_TOKEN:
|
|
case ERR_TOKEN:
|
|
answer.message = 'Invalid token';
|
|
answer.message = 'Invalid token';
|
|
break;
|
|
break;
|
|
|
|
+ case ERR_SERV:
|
|
|
|
+ answer.message = 'Server error';
|
|
|
|
+ break;
|
|
default:
|
|
default:
|
|
answer.message = 'Unknow error';
|
|
answer.message = 'Unknow error';
|
|
}
|
|
}
|
|
@@ -93,6 +102,21 @@ app.post('/token', (req, res) => {
|
|
res.json(success({valid: auth.verify(req.body.user, req.body.token)}));
|
|
res.json(success({valid: auth.verify(req.body.user, req.body.token)}));
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+app.post('/list', [verifyAuth, (req, res) => {
|
|
|
|
+ db.listFile(req.body.user).then((list) => {
|
|
|
|
+ console.log(list.length);
|
|
|
|
+ console.log(list);
|
|
|
|
+ if(list === false) {
|
|
|
|
+ res.json(error(ERR_SERV));
|
|
|
|
+ } else {
|
|
|
|
+ res.json(success({
|
|
|
|
+ total: list.length,
|
|
|
|
+ list: list
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+}]);
|
|
|
|
+
|
|
/*
|
|
/*
|
|
app.get('/', function (req, res) {
|
|
app.get('/', function (req, res) {
|
|
res.send('Hello World!');
|
|
res.send('Hello World!');
|