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
)
23 (!o
.name
|| !!(o
.name
.match(/^[\w-]+$/))) &&
24 (!o
.email
|| !!(o
.email
.match(/^[\w.+-]+@[\w.+-]+$/)))
28 create: function(name
, email
, notify
, cb
)
30 db
.serialize(function() {
32 "INSERT INTO Users " +
33 "(name, email, notify, created) VALUES " +
34 "('" + name
+ "','" + email
+ "'," + notify
+ "," + Date
.now() + ")";
35 db
.run(query
, function(err
) {
36 cb(err
, {uid: this.lastID
});
41 // Find one user by id, name, email, or token
42 getOne: function(by
, value
, cb
)
44 const delimiter
= (typeof value
=== "string" ? "'" : "");
45 db
.serialize(function() {
49 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
54 getByIds: function(ids
, cb
) {
55 db
.serialize(function() {
59 "WHERE id IN (" + ids
+ ")";
67 setLoginToken: function(token
, uid
)
69 db
.serialize(function() {
72 "SET loginToken = '" + token
+ "',loginTime = " + Date
.now() + " " +
78 // Set session token only if empty (first login)
79 // NOTE: weaker security (but avoid to re-login everywhere after each logout)
80 // TODO: option would be to reset all tokens periodically, e.g. every 3 months
81 trySetSessionToken: function(uid
, cb
)
83 db
.serialize(function() {
85 "SELECT sessionToken " +
88 db
.get(query
, (err
,ret
) => {
89 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
92 // Also empty the login token to invalidate future attempts
93 "SET loginToken = NULL" +
94 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "") + " " +
102 updateSettings: function(user
)
104 db
.serialize(function() {
107 "SET name = '" + user
.name
+ "'" +
108 ", email = '" + user
.email
+ "'" +
109 ", notify = " + user
.notify
+ " " +
110 "WHERE id = " + user
.id
;
118 notify: function(user
, message
)
120 const subject
= "vchess.club - notification";
121 const body
= "Hello " + user
.name
+ "!" + `
123 sendEmail(params
.mail
.noreply
, user
.email
, subject
, body
);
126 tryNotify: function(id
, message
)
128 UserModel
.getOne("id", id
, (err
,user
) => {
129 if (!err
&& user
.notify
)
130 UserModel
.notify(user
, message
);
137 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
) => {
148 // Remove unlogged users for > 24h
149 if (!u
.sessionToken
&& tsNow
- u
.created
> day
)
153 "Your account has been deleted because " +
154 "you didn't log in for 24h after registration"
156 db
.run("DELETE FROM Users WHERE id = " + u
.id
);
164 module
.exports
= UserModel
;