Finish fixing api.js

This commit is contained in:
calzoneman 2013-08-16 13:19:00 -05:00
parent f46169fbe3
commit 3f26fc80e0
1 changed files with 136 additions and 89 deletions

83
api.js
View File

@ -401,11 +401,10 @@ module.exports = function (Server) {
var text = req.body.profile_text; var text = req.body.profile_text;
db.userLoginSession(name, session, function (err, row) { db.userLoginSession(name, session, function (err, row) {
var row = Auth.login(name, pw, session); if(err) {
if(!row) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "Invalid login" error: err
}); });
return; return;
} }
@ -437,6 +436,7 @@ module.exports = function (Server) {
} }
}); });
}); });
});
/* set email */ /* set email */
app.post("/api/account/email", function (req, res) { app.post("/api/account/email", function (req, res) {
@ -461,11 +461,11 @@ module.exports = function (Server) {
return; return;
} }
var row = Auth.login(name, pw, ""); db.userLoginPassword(name, pw, function (err, row) {
if(!row) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "Invalid login credentials" error: err
}); });
return; return;
} }
@ -486,6 +486,7 @@ module.exports = function (Server) {
}); });
}); });
}); });
});
/* my channels */ /* my channels */
app.get("/api/account/mychannels", function (req, res) { app.get("/api/account/mychannels", function (req, res) {
@ -493,11 +494,11 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row) { if(err) {
res.jsonp({ res.jsonp({
success: false, success: false,
error: "Invalid login" error: err
}); });
return; return;
} }
@ -518,6 +519,8 @@ module.exports = function (Server) {
}); });
}); });
});
/* END REGION */ /* END REGION */
/* REGION log reading */ /* REGION log reading */
@ -529,8 +532,18 @@ module.exports = function (Server) {
var session = req.query.session; var session = req.query.session;
var types = req.query.actions; var types = req.query.actions;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
if(err !== "Invalid session" &&
err !== "Session expired") {
res.send(500);
} else {
res.send(403);
}
return;
}
if(row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
@ -539,6 +552,7 @@ module.exports = function (Server) {
var actions = ActionLog.readLog(types); var actions = ActionLog.readLog(types);
res.jsonp(actions); res.jsonp(actions);
}); });
});
/* helper function to pipe the last N bytes of a file */ /* helper function to pipe the last N bytes of a file */
function pipeLast(res, file, len) { function pipeLast(res, file, len) {
@ -564,14 +578,25 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
if(err !== "Invalid session" &&
err !== "Session expired") {
res.send(500);
} else {
res.send(403);
}
return;
}
if(row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
pipeLast(res, "sys.log", 1048576); pipeLast(res, "sys.log", 1048576);
}); });
});
app.get("/api/logging/errorlog", function (req, res) { app.get("/api/logging/errorlog", function (req, res) {
res.type("text/plain"); res.type("text/plain");
@ -580,14 +605,25 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
if(err !== "Invalid session" &&
err !== "Session expired") {
res.send(500);
} else {
res.send(403);
}
return;
}
if(row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
pipeLast(res, "error.log", 1048576); pipeLast(res, "error.log", 1048576);
}); });
});
app.get("/api/logging/channels/:channel", function (req, res) { app.get("/api/logging/channels/:channel", function (req, res) {
res.type("text/plain"); res.type("text/plain");
@ -596,14 +632,24 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, "", session); db.userLoginSession(name, session, function (err, row) {
if(!row || row.global_rank < 255) { if(err) {
if(err !== "Invalid session" &&
err !== "Session expired") {
res.send(500);
} else {
res.send(403);
}
return;
}
if(row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
var chan = req.params.channel || ""; var chan = req.params.channel || "";
if(!chan.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(chan)) {
res.send(400); res.send(400);
return; return;
} }
@ -616,6 +662,7 @@ module.exports = function (Server) {
} }
}); });
}); });
});
return null; return null;
} }