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
= require('sanitize-html');
9 router
.get("/userbio", access
.ajax
, (req
,res
) => {
10 const uid
= req
.query
["id"];
11 if (!!(uid
.toString().match(/^[0-9]+$/))) {
12 UserModel
.getBio(uid
, (err
, bio
) => {
18 router
.put('/userbio', access
.logged
, access
.ajax
, (req
,res
) => {
19 const bio
= sanitizeHtml(req
.body
.bio
);
20 UserModel
.setBio(req
.userId
, bio
);
24 router
.post('/register', access
.unlogged
, access
.ajax
, (req
,res
) => {
25 const name
= req
.body
.name
;
26 const email
= req
.body
.email
;
27 const notify
= !!req
.body
.notify
;
28 if (UserModel
.checkNameEmail({name: name
, email: email
})) {
29 UserModel
.create(name
, email
, notify
, (err
, ret
) => {
31 const msg
= err
.code
== "SQLITE_CONSTRAINT"
32 ? "User name or email already in use"
33 : "User creation failed. Try again";
34 res
.json({errmsg: msg
});
41 setAndSendLoginToken("Welcome to " + params
.siteURL
, user
, res
);
48 // NOTE: this method is safe because the sessionToken must be guessed
49 router
.get("/whoami", access
.ajax
, (req
,res
) => {
50 const callback
= (user
) => {
64 if (!req
.cookies
.token
) callback(anonymous
);
65 else if (req
.cookies
.token
.match(/^[a-z0-9]+$/)) {
66 UserModel
.getOne("sessionToken", req
.cookies
.token
, (err
, user
) => {
67 callback(user
|| anonymous
);
72 // NOTE: this method is safe because only IDs and names are returned
73 router
.get("/users", access
.ajax
, (req
,res
) => {
74 const ids
= req
.query
["ids"];
75 // NOTE: slightly too permissive RegExp
76 if (ids
.match(/^([0-9]+,?)+$/)) {
77 UserModel
.getByIds(ids
, (err
, users
) => {
78 res
.json({ users:users
});
83 router
.put('/update', access
.logged
, access
.ajax
, (req
,res
) => {
84 const name
= req
.body
.name
;
85 const email
= req
.body
.email
;
86 if (UserModel
.checkNameEmail({name: name
, email: email
})) {
91 notify: !!req
.body
.notify
,
93 UserModel
.updateSettings(user
);
98 // Authentication-related methods:
100 // to: object user (to who we send an email)
101 function setAndSendLoginToken(subject
, to
, res
) {
102 // Set login token and send welcome(back) email with auth link
103 const token
= genToken(params
.token
.length
);
104 UserModel
.setLoginToken(token
, to
.id
);
106 "Hello " + to
.name
+ " !" + `
108 "Access your account here: " +
109 params
.siteURL
+ "/#/authenticate/" + token
+ `
111 "Token will expire in " + params
.token
.expire
/(1000*60) + " minutes."
112 sendEmail(params
.mail
.noreply
, to
.email
, subject
, body
);
115 router
.get('/sendtoken', access
.unlogged
, access
.ajax
, (req
,res
) => {
116 const nameOrEmail
= decodeURIComponent(req
.query
.nameOrEmail
);
117 const type
= (nameOrEmail
.indexOf('@') >= 0 ? "email" : "name");
118 if (UserModel
.checkNameEmail({[type
]: nameOrEmail
})) {
119 UserModel
.getOne(type
, nameOrEmail
, (err
,user
) => {
120 access
.checkRequest(res
, err
, user
, "Unknown user", () => {
121 setAndSendLoginToken("Token for " + params
.siteURL
, user
, res
);
128 router
.get('/authenticate', access
.unlogged
, access
.ajax
, (req
,res
) => {
129 if (!req
.query
.token
.match(/^[a-z0-9]+$/))
130 return res
.json({errmsg: "Bad token"});
131 UserModel
.getOne("loginToken", req
.query
.token
, (err
,user
) => {
132 access
.checkRequest(res
, err
, user
, "Invalid token", () => {
133 // If token older than params.tokenExpire, do nothing
134 if (Date
.now() > user
.loginTime
+ params
.token
.expire
)
135 res
.json({errmsg: "Token expired"});
137 // Generate session token (if not exists) + destroy login token
138 UserModel
.trySetSessionToken(user
.id
, (token
) => {
139 res
.cookie("token", token
, {
141 secure: !!params
.siteURL
.match(/^https/),
142 maxAge: params
.cookieExpire
,
156 router
.get('/logout', access
.logged
, access
.ajax
, (req
,res
) => {
157 res
.clearCookie("token");
161 module
.exports
= router
;