Fix authentication
[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 router.get("/whoami", access.ajax, (req,res) => {
11 const callback = (user) => {
12 return res.json({
13 name: user.name,
14 email: user.email,
15 id: user.id,
16 notify: user.notify,
17 });
18 };
19 const anonymous = {name:"", email:"", id:0, notify:false};
20 if (!req.cookies.token)
21 return callback(anonymous);
22 UserModel.getOne("sessionToken", req.cookies.token, function(err, user) {
23 if (!!err || !user)
24 callback(anonymous);
25 else (!!user)
26 callback(user);
27 });
28 });
29
30 // to: object user (to who we send an email)
31 function setAndSendLoginToken(subject, to, res)
32 {
33 // Set login token and send welcome(back) email with auth link
34 const token = genToken(params.token.length);
35 UserModel.setLoginToken(token, to.id, err => {
36 if (!!err)
37 return res.json({errmsg: err.toString()});
38 const body =
39 "Hello " + to.name + "!\n" +
40 "Access your account here: " +
41 params.siteURL + "/#/authenticate/" + token + "\\n" +
42 "Token will expire in " + params.token.expire/(1000*60) + " minutes."
43 sendEmail(params.mail.noreply, to.email, subject, body, err => {
44 res.json(err || {});
45 });
46 });
47 }
48
49 router.post('/register', access.unlogged, access.ajax, (req,res) => {
50 const name = req.body.name;
51 const email = req.body.email;
52 const notify = !!req.body.notify;
53 const error = UserModel.checkNameEmail({name: name, email: email});
54 if (!!error)
55 return res.json({errmsg: error});
56 UserModel.create(name, email, notify, (err,uid) => {
57 if (!!err)
58 return res.json({errmsg: err.toString()});
59 const user = {
60 id: uid["rowid"],
61 name: name,
62 email: email,
63 };
64 setAndSendLoginToken("Welcome to " + params.siteURL, user, res);
65 });
66 });
67
68 router.get('/sendtoken', access.unlogged, access.ajax, (req,res) => {
69 const nameOrEmail = decodeURIComponent(req.query.nameOrEmail);
70 const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
71 const error = UserModel.checkNameEmail({[type]: nameOrEmail});
72 if (!!error)
73 return res.json({errmsg: error});
74 UserModel.getOne(type, nameOrEmail, (err,user) => {
75 access.checkRequest(res, err, user, "Unknown user", () => {
76 setAndSendLoginToken("Token for " + params.siteURL, user, res);
77 });
78 });
79 });
80
81 router.get('/authenticate', access.unlogged, access.ajax, (req,res) => {
82 UserModel.getOne("loginToken", req.query.token, (err,user) => {
83 access.checkRequest(res, err, user, "Invalid token", () => {
84 // If token older than params.tokenExpire, do nothing
85 if (Date.now() > user.loginTime + params.token.expire)
86 return res.json({errmsg: "Token expired"});
87 // Generate session token (if not exists) + destroy login token
88 UserModel.trySetSessionToken(user.id, (err,token) => {
89 if (!!err)
90 return res.json({errmsg: err.toString()});
91 // Set cookie
92 res.cookie("token", token, {
93 httpOnly: true,
94 secure: !!params.siteURL.match(/^https/),
95 maxAge: params.cookieExpire,
96 });
97 res.json({
98 id: user.id,
99 name: user.name,
100 email: user.email,
101 notify: user.notify,
102 });
103 });
104 });
105 });
106 });
107
108 router.put('/update', access.logged, access.ajax, (req,res) => {
109 const name = req.body.name;
110 const email = req.body.email;
111 const error = UserModel.checkNameEmail({name: name, email: email});
112 if (!!error)
113 return res.json({errmsg: error});
114 const user = {
115 id: req.userId,
116 name: name,
117 email: email,
118 notify: !!req.body.notify,
119 };
120 UserModel.updateSettings(user, err => {
121 res.json(err ? {errmsg: err.toString()} : {});
122 });
123 });
124
125 router.get('/logout', access.logged, access.ajax, (req,res) => {
126 res.clearCookie("token");
127 res.json({});
128 });
129
130 module.exports = router;