cc104ab07ed5821f8bca8065b98d36463f5f17e5
1 const db
= require("../utils/database");
2 const genToken
= require("../utils/tokenGenerator");
3 const params
= require("../config/parameters");
4 const sendEmail
= require('../utils/mailer');
5 const { exec
} = require("child_process");
12 * loginToken: token on server only
13 * loginTime: datetime (validity)
14 * sessionToken: token in cookies for authentication
15 * notify: boolean (send email notifications for corr games)
22 checkNameEmail: function(o
) {
24 (!o
.name
|| !!(o
.name
.match(/^[\w-]+$/))) &&
25 (!o
.email
|| !!(o
.email
.match(/^[\w.+-]+@[\w.+-]+$/)))
29 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
, { id: this.lastID
});
41 // Find one user by id, name, email, or token
42 getOne: function(by
, value
, fields
, cb
) {
43 const delimiter
= (typeof value
=== "string" ? "'" : "");
44 db
.serialize(function() {
46 "SELECT " + fields
+ " " +
48 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
53 getByIds: function(ids
, cb
) {
54 db
.serialize(function() {
58 "WHERE id IN (" + ids
+ ")";
63 getBio: function(id
, cb
) {
64 db
.serialize(function() {
76 setLoginToken: function(token
, id
) {
77 db
.serialize(function() {
80 "SET loginToken = '" + token
+ "',loginTime = " + Date
.now() + " " +
86 setBio: function(id
, bio
) {
87 db
.serialize(function() {
96 // Set session token only if empty (first login)
97 // NOTE: weaker security (but avoid to re-login everywhere after each logout)
98 // TODO: option would be to reset all tokens periodically (every 3 months?)
99 trySetSessionToken: function(id
, cb
) {
100 db
.serialize(function() {
102 "SELECT sessionToken " +
105 db
.get(query
, (err
, ret
) => {
106 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
107 const setSessionToken
=
108 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "");
111 // Also empty the login token to invalidate future attempts
112 "SET loginToken = NULL, loginTime = NULL " +
113 setSessionToken
+ " " +
121 updateSettings: function(user
) {
122 db
.serialize(function() {
125 "SET name = '" + user
.name
+ "'" +
126 ", email = '" + user
.email
+ "'" +
127 ", notify = " + user
.notify
+ " " +
128 "WHERE id = " + user
.id
;
136 notify: function(user
, message
) {
137 const subject
= "vchess.club - notification";
138 const body
= "Hello " + user
.name
+ " !" + `
140 sendEmail(params
.mail
.noreply
, user
.email
, subject
, body
);
143 tryNotify: function(id
, message
) {
144 UserModel
.getOne("id", id
, "name, email, notify", (err
, user
) => {
145 if (!err
&& user
.notify
) UserModel
.notify(user
, message
);
152 cleanUsersDb: function() {
153 const tsNow
= Date
.now();
154 // 86400000 = 24 hours in milliseconds
155 const day
= 86400000;
156 db
.serialize(function() {
158 "SELECT id, sessionToken, created, name, email " +
160 db
.all(query
, (err
, users
) => {
163 // Remove users unlogged for > 24h
164 if (!u
.sessionToken
&& tsNow
- u
.created
> day
)
169 "Your account has been deleted because " +
170 "you didn't log in for 24h after registration"
174 if (toRemove
.length
> 0) {
175 const remArg
= toRemove
.join(",");
177 "DELETE FROM Users " +
178 "WHERE id IN (" + remArg
+ ")"
187 module
.exports
= UserModel
;