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