New short-term TODOs
[vchess.git] / server / routes / users.js
CommitLineData
582df349
BA
1// AJAX methods to get, create, update or delete a user
2
8d7e2786
BA
3var router = require("express").Router();
4var UserModel = require('../models/User');
0bd5933d 5var sendEmail = require('../utils/mailer');
badeb466 6var genToken = require("../utils/tokenGenerator");
8d7e2786 7var access = require("../utils/access");
0bd5933d 8var params = require("../config/parameters");
8d7e2786 9
58e7b94e 10// NOTE: this method is safe because the sessionToken must be guessed
a7f9f050
BA
11router.get("/whoami", access.ajax, (req,res) => {
12 const callback = (user) => {
13 return res.json({
14 name: user.name,
15 email: user.email,
16 id: user.id,
17 notify: user.notify,
18 });
19 };
20 const anonymous = {name:"", email:"", id:0, notify:false};
a7f9f050
BA
21 if (!req.cookies.token)
22 return callback(anonymous);
23 UserModel.getOne("sessionToken", req.cookies.token, function(err, user) {
24 if (!!err || !user)
25 callback(anonymous);
f21cd6d9 26 else
a7f9f050
BA
27 callback(user);
28 });
29});
30
58e7b94e 31// NOTE: this method is safe because only IDs and names are returned
bebcc8d4 32router.get("/users", access.ajax, (req,res) => {
ed9c9c37
BA
33 const ids = req.query["ids"];
34 UserModel.getByIds(ids, (err,users) => {
35 if (!!err)
36 return res.json({errmsg: err.toString()});
37 return res.json({users:users});
38 });
bebcc8d4
BA
39});
40
c018b304 41// to: object user (to who we send an email)
8d7e2786
BA
42function setAndSendLoginToken(subject, to, res)
43{
44 // Set login token and send welcome(back) email with auth link
badeb466 45 const token = genToken(params.token.length);
c018b304
BA
46 UserModel.setLoginToken(token, to.id, err => {
47 if (!!err)
48 return res.json({errmsg: err.toString()});
49 const body =
2be5d614 50 "Hello " + to.name + "!\\n" +
c018b304 51 "Access your account here: " +
1aeed627 52 params.siteURL + "/#/authenticate/" + token + "\\n" +
c018b304
BA
53 "Token will expire in " + params.token.expire/(1000*60) + " minutes."
54 sendEmail(params.mail.noreply, to.email, subject, body, err => {
deca03e8 55 res.json(err || {});
8d7e2786
BA
56 });
57 });
58}
59
8d7e2786 60router.post('/register', access.unlogged, access.ajax, (req,res) => {
8a477a7e
BA
61 const name = req.body.name;
62 const email = req.body.email;
63 const notify = !!req.body.notify;
98db2082 64 const error = UserModel.checkNameEmail({name: name, email: email});
8a477a7e 65 if (!!error)
8d7e2786 66 return res.json({errmsg: error});
c018b304
BA
67 UserModel.create(name, email, notify, (err,uid) => {
68 if (!!err)
69 return res.json({errmsg: err.toString()});
70 const user = {
71 id: uid["rowid"],
72 name: name,
73 email: email,
74 };
75 setAndSendLoginToken("Welcome to " + params.siteURL, user, res);
8d7e2786
BA
76 });
77});
78
8a477a7e
BA
79router.get('/sendtoken', access.unlogged, access.ajax, (req,res) => {
80 const nameOrEmail = decodeURIComponent(req.query.nameOrEmail);
81 const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
98db2082 82 const error = UserModel.checkNameEmail({[type]: nameOrEmail});
8a477a7e 83 if (!!error)
8d7e2786 84 return res.json({errmsg: error});
8a477a7e 85 UserModel.getOne(type, nameOrEmail, (err,user) => {
8d7e2786
BA
86 access.checkRequest(res, err, user, "Unknown user", () => {
87 setAndSendLoginToken("Token for " + params.siteURL, user, res);
88 });
89 });
90});
91
1aeed627
BA
92router.get('/authenticate', access.unlogged, access.ajax, (req,res) => {
93 UserModel.getOne("loginToken", req.query.token, (err,user) => {
8d7e2786 94 access.checkRequest(res, err, user, "Invalid token", () => {
98f48579 95 // If token older than params.tokenExpire, do nothing
0bd5933d 96 if (Date.now() > user.loginTime + params.token.expire)
8d7e2786 97 return res.json({errmsg: "Token expired"});
0bd5933d 98 // Generate session token (if not exists) + destroy login token
c018b304 99 UserModel.trySetSessionToken(user.id, (err,token) => {
8d7e2786 100 if (!!err)
c018b304 101 return res.json({errmsg: err.toString()});
8d7e2786 102 // Set cookie
a7f9f050 103 res.cookie("token", token, {
8d7e2786 104 httpOnly: true,
c018b304
BA
105 secure: !!params.siteURL.match(/^https/),
106 maxAge: params.cookieExpire,
8d7e2786 107 });
a7f9f050
BA
108 res.json({
109 id: user.id,
110 name: user.name,
111 email: user.email,
112 notify: user.notify,
113 });
8d7e2786
BA
114 });
115 });
116 });
117});
118
c018b304
BA
119router.put('/update', access.logged, access.ajax, (req,res) => {
120 const name = req.body.name;
121 const email = req.body.email;
98db2082 122 const error = UserModel.checkNameEmail({name: name, email: email});
8a477a7e
BA
123 if (!!error)
124 return res.json({errmsg: error});
c018b304
BA
125 const user = {
126 id: req.userId,
127 name: name,
128 email: email,
129 notify: !!req.body.notify,
130 };
131 UserModel.updateSettings(user, err => {
132 res.json(err ? {errmsg: err.toString()} : {});
8d7e2786
BA
133 });
134});
135
1aeed627 136router.get('/logout', access.logged, access.ajax, (req,res) => {
0bd5933d 137 res.clearCookie("token");
1aeed627 138 res.json({});
8d7e2786
BA
139});
140
141module.exports = router;