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