1 var db
= require("../utils/database");
2 var genToken
= require("../utils/tokenGenerator");
3 var params
= require("../config/parameters");
4 var 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 if (typeof o
.name
=== "string")
24 if (o
.name
.length
== 0)
26 if (!o
.name
.match(/^[\w]+$/))
27 return "Bad characters in name";
29 if (typeof o
.email
=== "string")
31 if (o
.email
.length
== 0)
33 if (!o
.email
.match(/^[\w.+-]+@[\w.+-]+$/))
34 return "Bad characters in email";
38 // NOTE: parameters are already cleaned (in controller), thus no sanitization here
39 create: function(name
, email
, notify
, callback
)
41 db
.serialize(function() {
43 "INSERT INTO Users " +
44 "(name, email, notify, created) VALUES " +
45 "('" + name
+ "', '" + email
+ "', " + notify
+ "," + Date
.now() + ")";
46 db
.run(insertQuery
, err
=> {
49 db
.get("SELECT last_insert_rowid() AS rowid", callback
);
54 // Find one user (by id, name, email, or token)
55 getOne: function(by
, value
, cb
)
57 const delimiter
= (typeof value
=== "string" ? "'" : "");
58 db
.serialize(function() {
62 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
67 getByIds: function(ids
, cb
) {
68 db
.serialize(function() {
72 "WHERE id IN (" + ids
+ ")";
80 setLoginToken: function(token
, uid
, cb
)
82 db
.serialize(function() {
85 "SET loginToken = '" + token
+ "', loginTime = " + Date
.now() + " " +
91 // Set session token only if empty (first login)
92 // TODO: weaker security (but avoid to re-login everywhere after each logout)
93 trySetSessionToken: function(uid
, cb
)
95 // Also empty the login token to invalidate future attempts
96 db
.serialize(function() {
97 const querySessionToken
=
98 "SELECT sessionToken " +
101 db
.get(querySessionToken
, (err
,ret
) => {
104 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
107 "SET loginToken = NULL" +
108 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "") + " " +
116 updateSettings: function(user
, cb
)
118 db
.serialize(function() {
121 "SET name = '" + user
.name
+ "'" +
122 ", email = '" + user
.email
+ "'" +
123 ", notify = " + user
.notify
+ " " +
124 "WHERE id = " + user
.id
;
132 tryNotify: function(oppId
, message
)
134 UserModel
.getOne("id", oppId
, (err
,opp
) => {
135 if (!err
|| !opp
.notify
)
136 return; //error is ignored here (TODO: should be logged)
137 const subject
= "vchess.club - notification";
138 const body
= "Hello " + opp
.name
+ "!\n" + message
;
139 sendEmail(params
.mail
.noreply
, opp
.email
, subject
, body
, err
=> {
148 cleanUsersDb: function()
150 const tsNow
= Date
.now();
151 // 86400000 = 24 hours in milliseconds
152 const day
= 86400000;
153 db
.serialize(function() {
155 "SELECT id, sessionToken, created " +
157 db
.all(query
, (err
, users
) => {
159 // Remove unlogged users for >1 day
160 if (!u
.sessionToken
&& tsNow
- u
.created
> day
)
161 db
.run("DELETE FROM Users WHERE id = " + u
.id
);
168 module
.exports
= UserModel
;