1 var UserModel
= require("../models/User");
5 // Prevent access to "users pages"
6 logged: function(req
, res
, next
) {
7 const callback
= () => {
9 res
.json({ errmsg: "Error: try to delete cookies" });
12 let loggedIn
= undefined;
13 if (!req
.cookies
.token
) {
17 UserModel
.getOne("sessionToken", req
.cookies
.token
, (err
, user
) => {
22 // Token in cookies presumably wrong: erase it
23 res
.clearCookie("token");
31 // Prevent access to "anonymous pages"
32 unlogged: function(req
, res
, next
) {
33 // Just a quick heuristic, which should be enough
34 const loggedIn
= !!req
.cookies
.token
;
35 if (loggedIn
) res
.json({ errmsg: "Error: try to delete cookies" });
39 // Prevent direct access to AJAX results
40 ajax: function(req
, res
, next
) {
41 if (!req
.xhr
) res
.json({ errmsg: "Unauthorized access" });
45 // Check for errors before callback (continue page loading). (TODO: name?)
46 checkRequest: function(res
, err
, out
, msg
, cb
) {
47 if (!!err
) res
.json({ errmsg: err
.errmsg
|| err
.toString() });
50 (Array
.isArray(out
) && out
.length
== 0) ||
51 (typeof out
=== "object" && Object
.keys(out
).length
== 0)
53 res
.json({ errmsg: msg
});