11753a0a718b0e4ee9bdf4e1529b4054f3dfd33b
1 var UserModel
= require("../models/User");
5 // Prevent access to "users pages"
6 logged: function(req
, res
, next
) {
7 const callback
= () => {
9 return res
.json({errmsg: "Not logged in"});
12 let loggedIn
= undefined;
13 if (!req
.cookies
.token
)
20 UserModel
.getOne("sessionToken", req
.cookies
.token
, function(err
, user
) {
24 req
.userName
= user
.name
;
29 // Token in cookies presumably wrong: erase it
30 res
.clearCookie("token");
31 res
.clearCookie("id");
32 res
.clearCookie("name");
40 // Prevent access to "anonymous pages"
41 unlogged: function(req
, res
, next
) {
42 // Just a quick heuristic, which should be enough
43 const loggedIn
= !!req
.cookies
.token
;
45 return res
.json({errmsg: "Already logged in"});
49 // Prevent direct access to AJAX results
50 ajax: function(req
, res
, next
) {
52 return res
.json({errmsg: "Unauthorized access"});
56 // Check for errors before callback (continue page loading). TODO: better name.
57 checkRequest: function(res
, err
, out
, msg
, cb
) {
59 return res
.json({errmsg: err
.errmsg
|| err
.toString()});
61 || (Array
.isArray(out
) && out
.length
== 0)
62 || (typeof out
=== "object" && Object
.keys(out
).length
== 0))
64 return res
.json({errmsg: msg
});