Commit | Line | Data |
---|---|---|
fe4c7e67 BA |
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"); | |
dd10eb93 BA |
7 | const sanitizeHtml = require('sanitize-html'); |
8 | ||
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) => { | |
13 | res.json(bio); | |
14 | }); | |
15 | } | |
16 | }); | |
17 | ||
18 | router.put('/userbio', access.logged, access.ajax, (req,res) => { | |
19 | const bio = sanitizeHtml(req.body.bio); | |
20 | UserModel.setBio(req.userId, bio); | |
21 | res.json({}); | |
22 | }); | |
8d7e2786 | 23 | |
866842c3 BA |
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; | |
0234201f BA |
28 | if (UserModel.checkNameEmail({name: name, email: email})) { |
29 | UserModel.create(name, email, notify, (err, ret) => { | |
30 | if (!!err) { | |
f0c68a04 BA |
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}); | |
80b38d46 BA |
35 | } |
36 | else { | |
866842c3 | 37 | const user = { |
0234201f | 38 | id: ret.id, |
866842c3 BA |
39 | name: name, |
40 | email: email, | |
41 | }; | |
42 | setAndSendLoginToken("Welcome to " + params.siteURL, user, res); | |
43 | res.json({}); | |
44 | } | |
45 | }); | |
46 | } | |
47 | }); | |
48 | ||
58e7b94e | 49 | // NOTE: this method is safe because the sessionToken must be guessed |
e57c4de4 | 50 | router.get("/whoami", access.ajax, (req,res) => { |
a7f9f050 | 51 | const callback = (user) => { |
866842c3 | 52 | res.json({ |
a7f9f050 BA |
53 | name: user.name, |
54 | email: user.email, | |
55 | id: user.id, | |
dd10eb93 | 56 | notify: user.notify |
a7f9f050 BA |
57 | }); |
58 | }; | |
5c026d9a BA |
59 | const anonymous = { |
60 | name: "", | |
61 | email: "", | |
62 | id: 0, | |
dd10eb93 | 63 | notify: false |
5c026d9a | 64 | }; |
0234201f BA |
65 | if (!req.cookies.token) callback(anonymous); |
66 | else if (req.cookies.token.match(/^[a-z0-9]+$/)) { | |
866842c3 BA |
67 | UserModel.getOne("sessionToken", req.cookies.token, (err, user) => { |
68 | callback(user || anonymous); | |
69 | }); | |
70 | } | |
a7f9f050 BA |
71 | }); |
72 | ||
58e7b94e | 73 | // NOTE: this method is safe because only IDs and names are returned |
bebcc8d4 | 74 | router.get("/users", access.ajax, (req,res) => { |
ed9c9c37 | 75 | const ids = req.query["ids"]; |
0234201f BA |
76 | // NOTE: slightly too permissive RegExp |
77 | if (ids.match(/^([0-9]+,?)+$/)) { | |
dd10eb93 BA |
78 | UserModel.getByIds(ids, (err, users) => { |
79 | res.json({ users:users }); | |
866842c3 BA |
80 | }); |
81 | } | |
82 | }); | |
83 | ||
84 | router.put('/update', access.logged, access.ajax, (req,res) => { | |
85 | const name = req.body.name; | |
86 | const email = req.body.email; | |
0234201f | 87 | if (UserModel.checkNameEmail({name: name, email: email})) { |
866842c3 BA |
88 | const user = { |
89 | id: req.userId, | |
90 | name: name, | |
91 | email: email, | |
92 | notify: !!req.body.notify, | |
93 | }; | |
94 | UserModel.updateSettings(user); | |
95 | res.json({}); | |
96 | } | |
bebcc8d4 BA |
97 | }); |
98 | ||
866842c3 BA |
99 | // Authentication-related methods: |
100 | ||
c018b304 | 101 | // to: object user (to who we send an email) |
0234201f | 102 | function setAndSendLoginToken(subject, to, res) { |
dac39588 BA |
103 | // Set login token and send welcome(back) email with auth link |
104 | const token = genToken(params.token.length); | |
866842c3 BA |
105 | UserModel.setLoginToken(token, to.id); |
106 | const body = | |
f53871db | 107 | "Hello " + to.name + " !" + ` |
a749972c | 108 | ` + |
866842c3 BA |
109 | "Access your account here: " + |
110 | params.siteURL + "/#/authenticate/" + token + ` | |
a749972c | 111 | ` + |
866842c3 BA |
112 | "Token will expire in " + params.token.expire/(1000*60) + " minutes." |
113 | sendEmail(params.mail.noreply, to.email, subject, body); | |
8d7e2786 BA |
114 | } |
115 | ||
8a477a7e | 116 | router.get('/sendtoken', access.unlogged, access.ajax, (req,res) => { |
dac39588 BA |
117 | const nameOrEmail = decodeURIComponent(req.query.nameOrEmail); |
118 | const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name"); | |
0234201f | 119 | if (UserModel.checkNameEmail({[type]: nameOrEmail})) { |
866842c3 BA |
120 | UserModel.getOne(type, nameOrEmail, (err,user) => { |
121 | access.checkRequest(res, err, user, "Unknown user", () => { | |
122 | setAndSendLoginToken("Token for " + params.siteURL, user, res); | |
123 | res.json({}); | |
124 | }); | |
dac39588 | 125 | }); |
866842c3 | 126 | } |
8d7e2786 BA |
127 | }); |
128 | ||
1aeed627 | 129 | router.get('/authenticate', access.unlogged, access.ajax, (req,res) => { |
99b7a14c BA |
130 | if (!req.query.token.match(/^[a-z0-9]+$/)) |
131 | return res.json({errmsg: "Bad token"}); | |
1aeed627 | 132 | UserModel.getOne("loginToken", req.query.token, (err,user) => { |
dac39588 | 133 | access.checkRequest(res, err, user, "Invalid token", () => { |
98f48579 | 134 | // If token older than params.tokenExpire, do nothing |
dac39588 | 135 | if (Date.now() > user.loginTime + params.token.expire) |
866842c3 | 136 | res.json({errmsg: "Token expired"}); |
0234201f | 137 | else { |
866842c3 BA |
138 | // Generate session token (if not exists) + destroy login token |
139 | UserModel.trySetSessionToken(user.id, (token) => { | |
140 | res.cookie("token", token, { | |
141 | httpOnly: true, | |
142 | secure: !!params.siteURL.match(/^https/), | |
143 | maxAge: params.cookieExpire, | |
144 | }); | |
145 | res.json({ | |
146 | id: user.id, | |
147 | name: user.name, | |
148 | email: user.email, | |
149 | notify: user.notify, | |
150 | }); | |
a7f9f050 | 151 | }); |
866842c3 | 152 | } |
dac39588 BA |
153 | }); |
154 | }); | |
8d7e2786 BA |
155 | }); |
156 | ||
1aeed627 | 157 | router.get('/logout', access.logged, access.ajax, (req,res) => { |
dac39588 BA |
158 | res.clearCookie("token"); |
159 | res.json({}); | |
8d7e2786 BA |
160 | }); |
161 | ||
162 | module.exports = router; |