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");
7 const sanitizeHtml_pkg
= require('sanitize-html');
10 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'li', 'b',
11 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', 'table',
12 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre'
14 function sanitizeHtml(text
) {
15 return sanitizeHtml_pkg(text
, { allowedTags: allowedTags
});
18 router
.get("/userbio", access
.ajax
, (req
,res
) => {
19 const uid
= req
.query
["id"];
20 if (!!(uid
.toString().match(/^[0-9]+$/))) {
21 UserModel
.getBio(uid
, (err
, bio
) => {
27 router
.put('/userbio', access
.logged
, access
.ajax
, (req
,res
) => {
28 const bio
= sanitizeHtml(req
.body
.bio
);
29 UserModel
.setBio(req
.userId
, bio
);
33 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
34 const name
= req
.body
.name
;
35 const email
= req
.body
.email
;
36 const notify
= !!req
.body
.notify
;
37 if (UserModel
.checkNameEmail({ name: name
, email: email
})) {
38 UserModel
.create(name
, email
, notify
, (err
, ret
) => {
40 const msg
= err
.code
== "SQLITE_CONSTRAINT"
41 ? "User name or email already in use"
42 : "User creation failed. Try again";
43 res
.json({ errmsg: msg
});
51 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
);
58 // NOTE: this method is safe because the sessionToken must be guessed
59 router
.get("/whoami", access
.ajax
, (req
,res
) => {
60 const callback
= (user
) => {
74 if (!req
.cookies
.token
) callback(anonymous
);
75 else if (req
.cookies
.token
.match(/^[a-z0-9]+$/)) {
77 "sessionToken", req
.cookies
.token
, "name, email, id, notify",
78 (err
, user
) => callback(user
|| anonymous
)
83 // NOTE: this method is safe because only IDs and names are returned
84 router
.get("/users", access
.ajax
, (req
,res
) => {
85 const ids
= req
.query
["ids"];
86 // NOTE: slightly too permissive RegExp
87 if (!!ids
&& !!ids
.match(/^([0-9]+,?)+$/)) {
88 UserModel
.getByIds(ids
, (err
, users
) => {
89 res
.json({ users: users
});
94 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
95 const name
= req
.body
.name
;
96 const email
= req
.body
.email
;
97 if (UserModel
.checkNameEmail({ name: name
, email: email
})) {
102 notify: !!req
.body
.notify
,
104 UserModel
.updateSettings(user
);
109 // Authentication-related methods:
111 // to: object user (to who we send an email)
112 function setAndSendLoginToken(subject
, to
) {
113 // Set login token and send welcome(back) email with auth link
114 const token
= genToken(params
.token
.length
);
115 UserModel
.setLoginToken(token
, to
.id
);
117 "Hello " + to
.name
+ " !" + `
119 "Access your account here: " +
120 params
.siteURL
+ "/#/authenticate/" + token
+ `
122 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
123 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
);
126 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
127 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
128 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
129 if (UserModel
.checkNameEmail({ [type
]: nameOrEmail
})) {
130 UserModel
.getOne(type
, nameOrEmail
, "id, name, email", (err
, user
) => {
131 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
132 setAndSendLoginToken("Token for " + params
.siteURL
, user
);
139 router
.get('/authenticate', access
.unlogged
, access
.ajax
, (req
,res
) => {
140 if (!req
.query
.token
.match(/^[a-z0-9]+$/))
141 return res
.json({ errmsg: "Bad token" });
143 "loginToken", req
.query
.token
, "id, name, email, notify",
145 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
146 // If token older than params.tokenExpire, do nothing
147 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
148 res
.json({ errmsg: "Token expired" });
150 // Generate session token (if not exists) + destroy login token
151 UserModel
.trySetSessionToken(user
.id
, (token
) => {
152 res
.cookie("token", token
, {
154 secure: !!params
.siteURL
.match(/^https/),
155 maxAge: params
.cookieExpire
,
165 router
.get('/logout', access
.logged
, access
.ajax
, (req
,res
) => {
166 res
.clearCookie("token");
170 module
.exports
= router
;