1 const db
= require("../utils/database");
8 * ** Strings, identification informations:
11 * initials : computed, Benjamin Auder --> ba ...etc
14 * timestamp: datetime (validity)
15 * ip: address of requesting machine
17 * sessionTokens (array): cookie identification
20 getInitialsByPrefix: function(prefix
, cb
)
23 { initials: new RegExp("^" + prefix
) },
24 { initials: 1, _id: 0 },
29 insert: function(newUser
, cb
)
31 db
.users
.insert(Object
.assign({},
41 getByLoginToken: function(token
, cb
)
44 { "loginToken.value": token
},
49 getBySessionToken: function(token
, cb
)
52 { sessionTokens: token
},
57 getById: function(uid
, cb
)
65 getByEmail: function(email
, cb
)
73 getByInitials: function(initials
, cb
)
76 { initials: initials
},
81 getUnlogged: function(cb
)
83 var tsNow
= new Date().getTime();
84 // 86400000 = 24 hours in milliseconds
86 db
.users
.find({}, (err
,userArray
) => {
87 let unlogged
= userArray
.filter( u
=> {
88 return u
.sessionTokens
.length
==0 && u
._id
.getTimestamp().getTime() + day
< tsNow
;
96 db
.users
.find({}, cb
);
99 setLoginToken: function(token
, uid
, ip
, cb
)
103 { $set: { loginToken: {
105 timestamp: new Date().getTime(),
113 setSessionToken: function(token
, uid
, cb
)
115 // Also empty the login token to invalidate future attempts
119 $set: { loginToken: {} },
120 $push: { sessionTokens: {
122 $slice: -7 //only allow 7 simultaneous connections per user (TODO?)
129 removeToken: function(uid
, token
, cb
)
133 { $pull: {sessionTokens: token
} },
138 // TODO: later, allow account removal
139 remove: function(uids
)
141 db
.users
.remove({_id: uids
});
145 module
.exports
= UserEntity
;