1 const db
= require("../utils/database");
2 const genToken
= require("../utils/tokenGenerator");
3 const params
= require("../config/parameters");
4 const sendEmail
= require('../utils/mailer');
11 * loginToken: token on server only
12 * loginTime: datetime (validity)
13 * sessionToken: token in cookies for authentication
14 * notify: boolean (send email notifications for corr games)
20 checkNameEmail: function(o
) {
22 (!o
.name
|| !!(o
.name
.match(/^[\w-]+$/))) &&
23 (!o
.email
|| !!(o
.email
.match(/^[\w.+-]+@[\w.+-]+$/)))
27 create: function(name
, email
, notify
, cb
) {
28 db
.serialize(function() {
30 "INSERT INTO Users " +
31 "(name, email, notify, created) VALUES " +
32 "('" + name
+ "','" + email
+ "'," + notify
+ "," + Date
.now() + ")";
33 db
.run(query
, function(err
) {
34 cb(err
, { id: this.lastID
});
39 // Find one user by id, name, email, or token
40 getOne: function(by
, value
, cb
) {
41 const delimiter
= (typeof value
=== "string" ? "'" : "");
42 db
.serialize(function() {
46 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
51 getByIds: function(ids
, cb
) {
52 db
.serialize(function() {
56 "WHERE id IN (" + ids
+ ")";
64 setLoginToken: function(token
, id
) {
65 db
.serialize(function() {
68 "SET loginToken = '" + token
+ "',loginTime = " + Date
.now() + " " +
74 setNewsRead: function(id
) {
75 db
.serialize(function() {
78 "SET newsRead = " + Date
.now() + " " +
84 // Set session token only if empty (first login)
85 // NOTE: weaker security (but avoid to re-login everywhere after each logout)
86 // TODO: option would be to reset all tokens periodically, e.g. every 3 months
87 trySetSessionToken: function(id
, cb
) {
88 db
.serialize(function() {
90 "SELECT sessionToken " +
93 db
.get(query
, (err
,ret
) => {
94 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
97 // Also empty the login token to invalidate future attempts
98 "SET loginToken = NULL" +
99 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "") + " " +
107 updateSettings: function(user
) {
108 db
.serialize(function() {
111 "SET name = '" + user
.name
+ "'" +
112 ", email = '" + user
.email
+ "'" +
113 ", notify = " + user
.notify
+ " " +
114 "WHERE id = " + user
.id
;
122 notify: function(user
, message
) {
123 const subject
= "vchess.club - notification";
124 const body
= "Hello " + user
.name
+ " !" + `
126 sendEmail(params
.mail
.noreply
, user
.email
, subject
, body
);
129 tryNotify: function(id
, message
) {
130 UserModel
.getOne("id", id
, (err
,user
) => {
131 if (!err
&& user
.notify
) UserModel
.notify(user
, message
);
138 cleanUsersDb: function() {
139 const tsNow
= Date
.now();
140 // 86400000 = 24 hours in milliseconds
141 const day
= 86400000;
142 db
.serialize(function() {
144 "SELECT id, sessionToken, created, name, email " +
146 db
.all(query
, (err
, users
) => {
149 // Remove users unlogged for > 24h
150 if (!u
.sessionToken
&& tsNow
- u
.created
> day
)
155 "Your account has been deleted because " +
156 "you didn't log in for 24h after registration"
160 if (toRemove
.length
> 0) {
162 "DELETE FROM Users " +
163 "WHERE id IN (" + toRemove
.join(",") + ")"
171 module
.exports
= UserModel
;