732353f2d75ee3b1fd8b902e60f0a7e290c975b0
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
, function(err
, user
) {
20 req
.userName
= user
.name
;
23 // Token in cookies presumably wrong: erase it
24 res
.clearCookie("token");
32 // Prevent access to "anonymous pages"
33 unlogged: function(req
, res
, next
) {
34 // Just a quick heuristic, which should be enough
35 const loggedIn
= !!req
.cookies
.token
;
36 if (loggedIn
) res
.json({errmsg: "Error: try to delete cookies"});
40 // Prevent direct access to AJAX results
41 ajax: function(req
, res
, next
) {
42 if (!req
.xhr
) res
.json({errmsg: "Unauthorized access"});
46 // Check for errors before callback (continue page loading). TODO: better name.
47 checkRequest: function(res
, err
, out
, msg
, cb
) {
48 if (!!err
) res
.json({errmsg: err
.errmsg
|| err
.toString()});
51 (Array
.isArray(out
) && out
.length
== 0) ||
52 (typeof out
=== "object" && Object
.keys(out
).length
== 0)
54 res
.json({errmsg: msg
});