forked from BenjaminRH/meteor-user-session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
110 lines (105 loc) · 3.22 KB
/
common.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
108
109
110
// This collection is where the UserSession variables are ultimately stored
UserSessionCollection = new Meteor.Collection('userSessionCollection');
// Anonymous user error
noUserError = function () {
console.log('You cannot use UserSession methods when there is no user logged in.');
}
// Missing userId error
noUserIdError = function () {
console.log('You cannot use UserSession methods on the server without a userId.');
}
//=======================
// = UserSession METHODS
//=======================
UserSession = {
set: function (key, value, userId) {
// Set a new variable in the user session
if (Meteor.userId() || Meteor.isServer) {
// If the user is logged in, update the variable in the collection
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
var existing = UserSessionCollection.findOne({ key: key, userId: userId});
var sv = { key: key, value: value, userId: userId };
if (existing) UserSessionCollection.update({ _id: existing._id }, { $set: sv });
else UserSessionCollection.insert(sv);
} else {
//XXX Maybe we should degrade to normal Session and sync on login
noUserError();
}
},
get: function (key, userId) {
// Get the value of a user session variable
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) return existing.value;
} else {
noUserError();
}
},
delete: function (key, userId) {
// Delete a user session variable, if it exists
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) UserSessionCollection.remove(existing._id);
} else {
noUserError();
}
},
equals: function (key, value, userId) {
// Test if a user session variable is equal to a value
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) return existing.value == value; //XXX Should this be ===
} else {
noUserError();
}
},
list: function (userId) {
// Get all the user session variables as an object
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
var existing = UserSessionCollection.findOne({ userId: userId});
if (existing) {
var list = {};
UserSessionCollection.find({ userId: userId }).forEach(function (sv) {
list[sv.key] = sv.value;
});
return list;
}
} else {
noUserError();
}
}
};