-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlinkCodeProvider.js
58 lines (45 loc) · 1.35 KB
/
linkCodeProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var config = require('./config');
LinkCodeProvider = function() {
this.validCodes = [];
};
LinkCodeProvider.prototype = {
// 6 chars for linking code
codeLen: 6,
getNewCode: function(uid) {
this.gc();
var code = Math.floor(Math.random() * Math.pow(10, this.codeLen)).toString();
code = (new Array(this.codeLen - code.length + 1)).join("0") + code;
this.validCodes.push({
code: code,
validUntil: (new Date()).getTime() + config.linkCodeValidFor * 1000,
ownerUid: uid
});
return {code: code, validFor: config.linkCodeValidFor};
},
gc: function() {
var now = (new Date()).getTime();
var newValidCodes = [];
for (var i = 0; i < this.validCodes.length; i++) {
if (now < this.validCodes[i].validUntil) {
newValidCodes.push(this.validCodes[i]);
}
}
this.validCodes = newValidCodes;
},
isCodeValid: function(code) {
var valid = false;
var ownerUid = null;
var now = (new Date()).getTime();
for (var i = 0; i < this.validCodes.length; i++) {
if (this.validCodes[i].code == code && now < this.validCodes[i].validUntil) {
this.validCodes[i].validUntil = 0;
valid = true;
ownerUid = this.validCodes[i].ownerUid;
break;
}
}
this.gc();
return [valid, ownerUid];
}
};
exports.LinkCodeProvider = LinkCodeProvider;