1 // AJAX methods to get, create, update or delete a user
3 var router
= require("express").Router();
4 var UserModel
= require('../models/User');
5 var sendEmail
= require('../utils/mailer');
6 var genToken
= require("../utils/tokenGenerator");
7 var access
= require("../utils/access");
8 var params
= require("../config/parameters");
10 // to: object user (to who we send an email)
11 function setAndSendLoginToken(subject
, to
, res
)
13 // Set login token and send welcome(back) email with auth link
14 const token
= genToken(params
.token
.length
);
15 UserModel
.setLoginToken(token
, to
.id
, err
=> {
17 return res
.json({errmsg: err
.toString()});
19 "Hello " + to
.name
+ "!\n" +
20 "Access your account here: " +
21 params
.siteURL
+ "/authenticate?token=" + token
+ "\\n" +
22 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
23 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
, err
=> {
24 // "id" is generally the only info missing on client side,
25 // but the name is also unknown if log-in with the email.
26 res
.json(err
|| {id: to
.id
, name: to
.name
});
31 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
32 const name
= req
.body
.name
;
33 const email
= req
.body
.email
;
34 const notify
= !!req
.body
.notify
;
35 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
37 return res
.json({errmsg: error
});
38 UserModel
.create(name
, email
, notify
, (err
,uid
) => {
40 return res
.json({errmsg: err
.toString()});
46 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
50 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
51 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
52 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
53 const error
= UserModel
.checkNameEmail({[type
]: nameOrEmail
});
55 return res
.json({errmsg: error
});
56 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
57 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
58 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
63 router
.get('/authenticate', access
.unlogged
, (req
,res
) => {
64 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
65 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
66 // If token older than params.tokenExpire, do nothing
67 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
68 return res
.json({errmsg: "Token expired"});
69 // Generate session token (if not exists) + destroy login token
70 UserModel
.trySetSessionToken(user
.id
, (err
,token
) => {
72 return res
.json({errmsg: err
.toString()});
74 res
.cookie("token", token
, {
76 secure: !!params
.siteURL
.match(/^https/),
77 maxAge: params
.cookieExpire
,
85 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
86 const name
= req
.body
.name
;
87 const email
= req
.body
.email
;
88 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
90 return res
.json({errmsg: error
});
95 notify: !!req
.body
.notify
,
97 UserModel
.updateSettings(user
, err
=> {
98 res
.json(err
? {errmsg: err
.toString()} : {});
102 // Logout on server because the token cookie is httpOnly
103 router
.get('/logout', access
.logged
, (req
,res
) => {
104 res
.clearCookie("token");
108 module
.exports
= router
;