1 var UserModel
= require("../models/User");
4 // Prevent access to "users pages"
5 logged: function(req
, res
, next
) {
6 const callback
= () => {
8 res
.json({ errmsg: "Error: try to delete cookies" });
11 let loggedIn
= undefined;
12 if (!req
.cookies
.token
) {
17 "sessionToken", req
.cookies
.token
, "id",
23 // Token in cookies presumably wrong: erase it
24 res
.clearCookie("token");
33 // Prevent access to "anonymous pages"
34 unlogged: function(req
, res
, next
) {
35 // Just a quick heuristic, which should be enough
36 const loggedIn
= !!req
.cookies
.token
;
37 if (loggedIn
) res
.json({ errmsg: "Error: try to delete cookies" });
41 // Prevent direct access to AJAX results
42 ajax: function(req
, res
, next
) {
43 if (!req
.xhr
) res
.json({ errmsg: "Unauthorized access" });
47 // Check for errors before callback (continue page loading). (TODO: name?)
48 checkRequest: function(res
, err
, out
, msg
, cb
) {
49 if (!!err
) res
.json({ errmsg: err
.errmsg
|| err
.toString() });
52 (Array
.isArray(out
) && out
.length
== 0) ||
53 (typeof out
=== "object" && Object
.keys(out
).length
== 0)
55 res
.json({ errmsg: msg
});