a36ab683487fd7d9d6bc8761ee105536e0c9ca42
1 var db
= require("../utils/database");
2 var maild
= require("../utils/mailer.js");
3 var genToken
= require("../utils/tokenGenerator");
4 var params
= require("../config/parameters");
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)
19 // NOTE: parameters are already cleaned (in controller), thus no sanitization here
20 create: function(name
, email
, notify
, callback
)
22 db
.serialize(function() {
24 "INSERT INTO Users " +
25 "(name, email, notify) VALUES " +
26 "('" + name
+ "', '" + email
+ "', " + notify
+ ")";
27 db
.run(insertQuery
, err
=> {
30 db
.get("SELECT last_insert_rowid() AS rowid", callback
);
35 // Find one user (by id, name, email, or token)
36 getOne: function(by
, value
, cb
)
38 const delimiter
= (typeof value
=== "string" ? "'" : "");
39 db
.serialize(function() {
43 "WHERE " + by
+ " = " + delimiter
+ value
+ delimiter
;
51 setLoginToken: function(token
, uid
, cb
)
53 db
.serialize(function() {
56 "SET loginToken = '" + token
+ "', loginTime = " + Date
.now() + " " +
62 // Set session token only if empty (first login)
63 // TODO: weaker security (but avoid to re-login everywhere after each logout)
64 trySetSessionToken: function(uid
, cb
)
66 // Also empty the login token to invalidate future attempts
67 db
.serialize(function() {
68 const querySessionToken
=
69 "SELECT sessionToken " +
72 db
.get(querySessionToken
, (err
,ret
) => {
75 const token
= ret
.sessionToken
|| genToken(params
.token
.length
);
78 "SET loginToken = NULL" +
79 (!ret
.sessionToken
? (", sessionToken = '" + token
+ "'") : "") + " " +
87 updateSettings: function(user
, cb
)
89 db
.serialize(function() {
92 "SET name = '" + user
.name
+ "'" +
93 ", email = '" + user
.email
+ "'" +
94 ", notify = " + user
.notify
+ " " +
95 "WHERE id = " + user
.id
;
101 module
.exports
= UserModel
;