88aab68ce01bf25e1fbc3acad5fe211dd4f470ee
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
) {
18 "sessionToken", req
.cookies
.token
, "id",
24 // Token in cookies presumably wrong: erase it
25 res
.clearCookie("token");
34 // Prevent access to "anonymous pages"
35 unlogged: function(req
, res
, next
) {
36 // Just a quick heuristic, which should be enough
37 const loggedIn
= !!req
.cookies
.token
;
38 if (loggedIn
) res
.json({ errmsg: "Error: try to delete cookies" });
42 // Prevent direct access to AJAX results
43 ajax: function(req
, res
, next
) {
44 if (!req
.xhr
) res
.json({ errmsg: "Unauthorized access" });
48 // Check for errors before callback (continue page loading). (TODO: name?)
49 checkRequest: function(res
, err
, out
, msg
, cb
) {
50 if (!!err
) res
.json({ errmsg: err
.errmsg
|| err
.toString() });
53 (Array
.isArray(out
) && out
.length
== 0) ||
54 (typeof out
=== "object" && Object
.keys(out
).length
== 0)
56 res
.json({ errmsg: msg
});