-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (94 loc) · 2.07 KB
/
server.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
export const logins = new Mongo.Collection('logins')
import platform from 'platform'
const diffBy = function (a, b, key) {
const res = []
a.forEach(function (x) {
if (b.find((z) => x[key] === z[key]) === undefined) {
res.push(x)
}
})
return res
}
Meteor.users.find({}, {
fields: {
_id: 1,
'services.resume.loginTokens': 1,
},
}).observe({
changed(newDoc, oldDoc) {
const oldTokens = oldDoc.services.resume.loginTokens
const newTokens = newDoc.services.resume.loginTokens
const addedLogins = diffBy(newTokens, oldTokens, 'hashedToken')
addedLogins.forEach((token) => {
logins.insert({
hashedToken: token.hashedToken,
creation: token.when,
userId: newDoc._id,
})
})
const removedLogins = diffBy(oldTokens, newTokens, 'hashedToken')
removedLogins.forEach((token) => {
logins.remove({
hashedToken: token.hashedToken,
})
})
},
})
Meteor.methods({
'logins_updateLastLogin': function (token) {
check(token, String)
const hashed = Accounts._hashLoginToken(token)
const login = logins.findOne({
hashedToken: hashed,
})
if (login == null) {
throw new Meteor.Error('not-found')
}
const userAgent = this.connection.httpHeaders['user-agent']
const info = platform.parse(userAgent)
if (info.os && info.os.family.indexOf('Windows') === 0) {
info.os.family = 'Windows'
}
logins.update(login._id, {
$set: {
lastLogin: {
ip: this.connection.clientAddress,
date: new Date(),
...info,
},
},
})
return login._id
},
'logins_kill': function (id) {
check(id, String)
if (this.userId == null) {
throw new Meteor.Error('not-logged-in')
}
const login = logins.findOne(id)
if (login == null) {
throw new Meteor.Error('not-found')
}
Meteor.users.update(this.userId, {
$pull: {
'services.resume.loginTokens': {
hashedToken: login.hashedToken,
},
},
})
},
})
Meteor.publish('logins', function () {
if (!this.userId) {
this.ready()
return undefined
}
return logins.find({
userId: this.userId,
}, {
fields: {
hashedToken: 0,
},
})
})