Separate client and server codes. Keep everything in one git repo for simplicity
[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");
625022fd 9var checkNameEmail = require("../data/userCheck")
8d7e2786 10
c018b304 11// to: object user (to who we send an email)
8d7e2786
BA
12function setAndSendLoginToken(subject, to, res)
13{
14 // Set login token and send welcome(back) email with auth link
badeb466 15 const token = genToken(params.token.length);
c018b304
BA
16 UserModel.setLoginToken(token, to.id, err => {
17 if (!!err)
18 return res.json({errmsg: err.toString()});
19 const body =
20 "Hello " + to.name + "!\n" +
21 "Access your account here: " +
22 params.siteURL + "/authenticate?token=" + token + "\\n" +
23 "Token will expire in " + params.token.expire/(1000*60) + " minutes."
24 sendEmail(params.mail.noreply, to.email, subject, body, err => {
625022fd
BA
25 // "id" is generally the only info missing on client side,
26 // but the name is also unknown if log-in with the email.
27 res.json(err || {id: to.id, name: to.name});
8d7e2786
BA
28 });
29 });
30}
31
8d7e2786 32router.post('/register', access.unlogged, access.ajax, (req,res) => {
8a477a7e
BA
33 const name = req.body.name;
34 const email = req.body.email;
35 const notify = !!req.body.notify;
36 const error = checkNameEmail({name: name, email: email});
37 if (!!error)
8d7e2786 38 return res.json({errmsg: error});
c018b304
BA
39 UserModel.create(name, email, notify, (err,uid) => {
40 if (!!err)
41 return res.json({errmsg: err.toString()});
42 const user = {
43 id: uid["rowid"],
44 name: name,
45 email: email,
46 };
47 setAndSendLoginToken("Welcome to " + params.siteURL, user, res);
8d7e2786
BA
48 });
49});
50
8a477a7e
BA
51router.get('/sendtoken', access.unlogged, access.ajax, (req,res) => {
52 const nameOrEmail = decodeURIComponent(req.query.nameOrEmail);
53 const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
54 const error = checkNameEmail({[type]: nameOrEmail});
55 if (!!error)
8d7e2786 56 return res.json({errmsg: error});
8a477a7e 57 UserModel.getOne(type, nameOrEmail, (err,user) => {
8d7e2786
BA
58 access.checkRequest(res, err, user, "Unknown user", () => {
59 setAndSendLoginToken("Token for " + params.siteURL, user, res);
60 });
61 });
62});
63
64router.get('/authenticate', access.unlogged, (req,res) => {
c018b304 65 UserModel.getOne("loginToken", req.query.token, (err,user) => {
8d7e2786 66 access.checkRequest(res, err, user, "Invalid token", () => {
8d7e2786 67 // If token older than params.tokenExpire, do nothing
0bd5933d 68 if (Date.now() > user.loginTime + params.token.expire)
8d7e2786 69 return res.json({errmsg: "Token expired"});
0bd5933d 70 // Generate session token (if not exists) + destroy login token
c018b304 71 UserModel.trySetSessionToken(user.id, (err,token) => {
8d7e2786 72 if (!!err)
c018b304 73 return res.json({errmsg: err.toString()});
8d7e2786
BA
74 // Set cookie
75 res.cookie("token", token, {
76 httpOnly: true,
c018b304
BA
77 secure: !!params.siteURL.match(/^https/),
78 maxAge: params.cookieExpire,
8d7e2786
BA
79 });
80 res.redirect("/");
81 });
82 });
83 });
84});
85
c018b304
BA
86router.put('/update', access.logged, access.ajax, (req,res) => {
87 const name = req.body.name;
88 const email = req.body.email;
89 const error = checkNameEmail({name: name, email: email});
8a477a7e
BA
90 if (!!error)
91 return res.json({errmsg: error});
c018b304
BA
92 const user = {
93 id: req.userId,
94 name: name,
95 email: email,
96 notify: !!req.body.notify,
97 };
98 UserModel.updateSettings(user, err => {
99 res.json(err ? {errmsg: err.toString()} : {});
8d7e2786
BA
100 });
101});
102
c018b304 103// Logout on server because the token cookie is httpOnly
8d7e2786 104router.get('/logout', access.logged, (req,res) => {
0bd5933d
BA
105 res.clearCookie("token");
106 res.redirect('/');
8d7e2786
BA
107});
108
109module.exports = router;