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
, fields
, cb
) {
41 const delimiter
= (typeof value
=== "string" ? "'" : "");
42 db
.serialize(function() {
44 "SELECT " + fields
+ " " +
46 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
51 getByIds: function(ids
, cb
) {
52 db
.serialize(function() {
56 "WHERE id IN (" + ids
+ ")";
61 getBio: function(id
, cb
) {
62 db
.serialize(function() {
74 setLoginToken: function(token
, id
) {
75 db
.serialize(function() {
78 "SET loginToken = '" + token
+ "',loginTime = " + Date
.now() + " " +
84 setBio: function(id
, bio
) {
85 db
.serialize(function() {
94 // Set session token only if empty (first login)
95 // NOTE: weaker security (but avoid to re-login everywhere after each logout)
96 // TODO: option would be to reset all tokens periodically (every 3 months?)
97 trySetSessionToken: function(id
, cb
) {
98 db
.serialize(function() {
100 "SELECT sessionToken " +
103 db
.get(query
, (err
, ret
) => {
104 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
105 const setSessionToken
=
106 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "");
109 // Also empty the login token to invalidate future attempts
110 "SET loginToken = NULL, loginTime = NULL " +
111 setSessionToken
+ " " +
119 updateSettings: function(user
) {
120 db
.serialize(function() {
123 "SET name = '" + user
.name
+ "'" +
124 ", email = '" + user
.email
+ "'" +
125 ", notify = " + user
.notify
+ " " +
126 "WHERE id = " + user
.id
;
134 notify: function(user
, message
) {
135 const subject
= "vchess.club - notification";
136 const body
= "Hello " + user
.name
+ " !" + `
138 sendEmail(params
.mail
.noreply
, user
.email
, subject
, body
);
141 tryNotify: function(id
, message
) {
142 UserModel
.getOne("id", id
, "name, email, notify", (err
, user
) => {
143 if (!err
&& user
.notify
) UserModel
.notify(user
, message
);
150 cleanUsersDb: function() {
151 const tsNow
= Date
.now();
152 // 86400000 = 24 hours in milliseconds
153 const day
= 86400000;
154 db
.serialize(function() {
156 "SELECT id, sessionToken, created, name, email " +
158 db
.all(query
, (err
, users
) => {
161 // Remove users unlogged for > 24h
162 if (!u
.sessionToken
&& tsNow
- u
.created
> day
)
167 "Your account has been deleted because " +
168 "you didn't log in for 24h after registration"
172 if (toRemove
.length
> 0) {
174 "DELETE FROM Users " +
175 "WHERE id IN (" + toRemove
.join(",") + ")"
183 module
.exports
= UserModel
;