1 let router
= require("express").Router();
2 const UserModel
= require('../models/User');
3 const sendEmail
= require('../utils/mailer');
4 const genToken
= require("../utils/tokenGenerator");
5 const access
= require("../utils/access");
6 const params
= require("../config/parameters");
8 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
9 const name
= req
.body
.name
;
10 const email
= req
.body
.email
;
11 const notify
= !!req
.body
.notify
;
12 if (UserModel
.checkNameEmail({name: name
, email: email
}))
14 UserModel
.create(name
, email
, notify
, (err
,ret
) => {
16 res
.json({errmsg: "User creation failed. Try again"});
24 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
31 // NOTE: this method is safe because the sessionToken must be guessed
32 router
.get("/whoami", access
.ajax
, (req
,res
) => {
33 const callback
= (user
) => {
41 const anonymous
= {name:"", email:"", id:0, notify:false};
42 if (!req
.cookies
.token
)
44 else if (req
.cookies
.token
.match(/^[a-z0-9]+$/))
46 UserModel
.getOne("sessionToken", req
.cookies
.token
, (err
, user
) => {
47 callback(user
|| anonymous
);
52 // NOTE: this method is safe because only IDs and names are returned
53 router
.get("/users", access
.ajax
, (req
,res
) => {
54 const ids
= req
.query
["ids"];
55 if (ids
.match(/^([0-9]+,?)+$/)) //NOTE: slightly too permissive
57 UserModel
.getByIds(ids
, (err
,users
) => {
58 res
.json({users:users
});
63 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
64 const name
= req
.body
.name
;
65 const email
= req
.body
.email
;
66 if (UserModel
.checkNameEmail({name: name
, email: email
}));
72 notify: !!req
.body
.notify
,
74 UserModel
.updateSettings(user
);
79 // Authentication-related methods:
81 // to: object user (to who we send an email)
82 function setAndSendLoginToken(subject
, to
, res
)
84 // Set login token and send welcome(back) email with auth link
85 const token
= genToken(params
.token
.length
);
86 UserModel
.setLoginToken(token
, to
.id
);
88 "Hello " + to
.name
+ "!" + `
90 "Access your account here: " +
91 params
.siteURL
+ "/#/authenticate/" + token
+ `
93 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
94 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
);
97 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
98 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
99 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
100 if (UserModel
.checkNameEmail({[type
]: nameOrEmail
}))
102 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
103 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
104 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
111 router
.get('/authenticate', access
.unlogged
, access
.ajax
, (req
,res
) => {
112 if (!req
.query
.token
.match(/^[a-z0-9]+$/))
113 return res
.json({errmsg: "Bad token"});
114 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
115 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
116 // If token older than params.tokenExpire, do nothing
117 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
118 res
.json({errmsg: "Token expired"});
121 // Generate session token (if not exists) + destroy login token
122 UserModel
.trySetSessionToken(user
.id
, (token
) => {
123 res
.cookie("token", token
, {
125 secure: !!params
.siteURL
.match(/^https/),
126 maxAge: params
.cookieExpire
,
140 router
.get('/logout', access
.logged
, access
.ajax
, (req
,res
) => {
141 res
.clearCookie("token");
145 module
.exports
= router
;