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