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 router
.get("/whoami", access
.ajax
, (req
,res
) => {
11 const callback
= (user
) => {
19 const anonymous
= {name:"", email:"", id:0, notify:false};
20 if (!req
.cookies
.token
)
21 return callback(anonymous
);
22 UserModel
.getOne("sessionToken", req
.cookies
.token
, function(err
, user
) {
30 // to: object user (to who we send an email)
31 function setAndSendLoginToken(subject
, to
, res
)
33 // Set login token and send welcome(back) email with auth link
34 const token
= genToken(params
.token
.length
);
35 UserModel
.setLoginToken(token
, to
.id
, err
=> {
37 return res
.json({errmsg: err
.toString()});
39 "Hello " + to
.name
+ "!\n" +
40 "Access your account here: " +
41 params
.siteURL
+ "/#/authenticate/" + token
+ "\\n" +
42 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
43 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
, err
=> {
49 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
50 const name
= req
.body
.name
;
51 const email
= req
.body
.email
;
52 const notify
= !!req
.body
.notify
;
53 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
55 return res
.json({errmsg: error
});
56 UserModel
.create(name
, email
, notify
, (err
,uid
) => {
58 return res
.json({errmsg: err
.toString()});
64 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
68 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
69 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
70 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
71 const error
= UserModel
.checkNameEmail({[type
]: nameOrEmail
});
73 return res
.json({errmsg: error
});
74 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
75 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
76 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
81 router
.get('/authenticate', access
.unlogged
, access
.ajax
, (req
,res
) => {
82 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
83 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
84 // If token older than params.tokenExpire, do nothing
85 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
86 return res
.json({errmsg: "Token expired"});
87 // Generate session token (if not exists) + destroy login token
88 UserModel
.trySetSessionToken(user
.id
, (err
,token
) => {
90 return res
.json({errmsg: err
.toString()});
92 res
.cookie("token", token
, {
94 secure: !!params
.siteURL
.match(/^https/),
95 maxAge: params
.cookieExpire
,
108 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
109 const name
= req
.body
.name
;
110 const email
= req
.body
.email
;
111 const error
= UserModel
.checkNameEmail({name: name
, email: email
});
113 return res
.json({errmsg: error
});
118 notify: !!req
.body
.notify
,
120 UserModel
.updateSettings(user
, err
=> {
121 res
.json(err
? {errmsg: err
.toString()} : {});
125 router
.get('/logout', access
.logged
, access
.ajax
, (req
,res
) => {
126 res
.clearCookie("token");
130 module
.exports
= router
;