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