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
=> {
25 console
.log("send login infos ::");
28 // "id" is generally the only info missing on client side,
29 // but the name is also unknown if log-in with the email.
30 res
.json(err
|| {id: to
.id
, name: to
.name
});
35 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
36 const name
= req
.body
.name
;
37 const email
= req
.body
.email
;
38 const notify
= !!req
.body
.notify
;
39 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
41 return res
.json({errmsg: error
});
42 UserModel
.create(name
, email
, notify
, (err
,uid
) => {
44 return res
.json({errmsg: err
.toString()});
50 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
54 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
55 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
56 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
57 const error
= UserModel
.checkNameEmail({[type
]: nameOrEmail
});
59 return res
.json({errmsg: error
});
60 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
61 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
62 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
67 router
.get('/authenticate', access
.unlogged
, (req
,res
) => {
68 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
69 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
70 // If token older than params.tokenExpire, do nothing
71 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
72 return res
.json({errmsg: "Token expired"});
73 // Generate session token (if not exists) + destroy login token
74 UserModel
.trySetSessionToken(user
.id
, (err
,token
) => {
76 return res
.json({errmsg: err
.toString()});
78 res
.cookie("token", token
, {
80 secure: !!params
.siteURL
.match(/^https/),
81 maxAge: params
.cookieExpire
,
89 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
90 const name
= req
.body
.name
;
91 const email
= req
.body
.email
;
92 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
94 return res
.json({errmsg: error
});
99 notify: !!req
.body
.notify
,
101 UserModel
.updateSettings(user
, err
=> {
102 res
.json(err
? {errmsg: err
.toString()} : {});
106 // Logout on server because the token cookie is httpOnly
107 router
.get('/logout', access
.logged
, (req
,res
) => {
108 res
.clearCookie("token");
112 module
.exports
= router
;