1 var db
= require("../utils/database");
2 var maild
= require("../utils/mailer.js");
3 var TokenGen
= require("../utils/tokenGenerator");
10 * loginToken: token on server only
11 * loginTime: datetime (validity)
12 * sessionToken: token in cookies for authentication
13 * notify: boolean (send email notifications for corr games)
17 exports
.create = function(name
, email
, notify
, callback
)
19 db
.serialize(function() {
21 "INSERT INTO Users " +
22 "(name, email, notify) VALUES " +
23 "('" + name
+ "', '" + email
+ "', " + notify
+ ")";
24 db
.run(query
, callback
); //TODO: need to get the inserted user (how ?)
28 // Find one user (by id, name, email, or token)
29 exports
.getOne = function(by
, value
, cb
)
31 const delimiter
= (typeof value
=== "string" ? "'" : "");
32 db
.serialize(function() {
34 "SELECT * FROM Users " +
35 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
43 exports
.setLoginToken = function(token
, uid
, cb
)
45 db
.serialize(function() {
48 "SET loginToken = " + token
+ " AND loginTime = " + Date
.now() + " " +
54 // Set session token only if empty (first login)
55 // TODO: weaker security (but avoid to re-login everywhere after each logout)
56 exports
.trySetSessionToken = function(uid
, cb
)
58 // Also empty the login token to invalidate future attempts
59 db
.serialize(function() {
60 const querySessionTOken
=
61 "SELECT sessionToken " +
64 db
.get(querySessionToken
, (err
,token
) => {
67 const newToken
= token
|| TokenGen
.generate(params
.token
.length
);
70 "SET loginToken = NULL " +
71 (!token
? "AND sessionToken = " + newToken
+ " " : "") +
79 exports
.updateSettings = function(user
, cb
)
81 db
.serialize(function() {
84 "SET name = " + user
.name
+
85 " AND email = " + user
.email
+
86 " AND notify = " + user
.notify
+ " " +
87 "WHERE id = " + user
._id
;