1 // AJAX methods to get, create, update or delete a user
3 let router
= require("express").Router();
4 const UserModel
= require('../models/User');
5 const sendEmail
= require('../utils/mailer');
6 const genToken
= require("../utils/tokenGenerator");
7 const access
= require("../utils/access");
8 const params
= require("../config/parameters");
10 // NOTE: this method is safe because the sessionToken must be guessed
11 router
.get("/whoami", access
.ajax
, (req
,res
) => {
12 const callback
= (user
) => {
20 const anonymous
= {name:"", email:"", id:0, notify:false};
21 if (!req
.cookies
.token
)
22 return callback(anonymous
);
23 if (!req
.cookies
.token
.match(/^[a-z0-9]+$/))
24 return res
.json({errmsg: "Bad token"});
25 UserModel
.getOne("sessionToken", req
.cookies
.token
, function(err
, user
) {
33 // NOTE: this method is safe because only IDs and names are returned
34 router
.get("/users", access
.ajax
, (req
,res
) => {
35 const ids
= req
.query
["ids"];
36 if (!!ids
&& !ids
.match(/^([0-9]+,?)+$/)) //NOTE: slightly too permissive
37 return res
.json({errmsg: "Bad IDs array"});
38 UserModel
.getByIds(ids
, (err
,users
) => {
40 return res
.json({errmsg: err
.toString()});
41 return res
.json({users:users
});
45 // to: object user (to who we send an email)
46 function setAndSendLoginToken(subject
, to
, res
)
48 // Set login token and send welcome(back) email with auth link
49 const token
= genToken(params
.token
.length
);
50 UserModel
.setLoginToken(token
, to
.id
, err
=> {
52 return res
.json({errmsg: err
.toString()});
54 "Hello " + to
.name
+ "!" + `
56 "Access your account here: " +
57 params
.siteURL
+ "/#/authenticate/" + token
+ `
59 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
60 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
, err
=> {
66 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
67 const name
= req
.body
.name
;
68 const email
= req
.body
.email
;
69 const notify
= !!req
.body
.notify
;
70 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
72 return res
.json({errmsg: error
});
73 UserModel
.create(name
, email
, notify
, (err
,uid
) => {
75 return res
.json({errmsg: err
.toString()});
81 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
85 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
86 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
87 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
88 const error
= UserModel
.checkNameEmail({[type
]: nameOrEmail
});
90 return res
.json({errmsg: error
});
91 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
92 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
93 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
98 router
.get('/authenticate', access
.unlogged
, access
.ajax
, (req
,res
) => {
99 if (!req
.query
.token
.match(/^[a-z0-9]+$/))
100 return res
.json({errmsg: "Bad token"});
101 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
102 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
103 // If token older than params.tokenExpire, do nothing
104 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
105 return res
.json({errmsg: "Token expired"});
106 // Generate session token (if not exists) + destroy login token
107 UserModel
.trySetSessionToken(user
.id
, (err
,token
) => {
109 return res
.json({errmsg: err
.toString()});
111 res
.cookie("token", token
, {
113 secure: !!params
.siteURL
.match(/^https/),
114 maxAge: params
.cookieExpire
,
127 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
128 const name
= req
.body
.name
;
129 const email
= req
.body
.email
;
130 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
132 return res
.json({errmsg: error
});
137 notify: !!req
.body
.notify
,
139 UserModel
.updateSettings(user
, err
=> {
140 res
.json(err
? {errmsg: err
.toString()} : {});
144 router
.get('/logout', access
.logged
, access
.ajax
, (req
,res
) => {
145 res
.clearCookie("token");
149 module
.exports
= router
;