forked from brettle/meteor-accounts-login-state
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts-login-state-server.js
142 lines (123 loc) · 4.02 KB
/
accounts-login-state-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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* globals ServiceConfiguration, Hook, LoginState: true */
"use strict";
function LoginStateConstructor() {
var self = this;
self._signedUpHook = new Hook({
debugPrintExceptions: "LoginState signedUp interceptor callback",
});
self._interceptorsChangedHook = new Hook({
debugPrintExceptions: "LoginState interceptorsChanged callback"
});
// If accounts-password is installed and the user has a username, or at
// least one email address, then they are signed up.
if (Package['accounts-password']) {
self.addSignedUpInterceptor(function (u) {
if (u && u.services && u.services.password &&
(typeof (u.username) === 'string' ||
(u.emails && u.emails[0] &&
typeof (u.emails[0].address) === 'string'))) {
u.loginStateSignedUp = true;
}
});
}
self.addSignedUpInterceptor(function (u) {
var usedServices = _.keys(u.services);
var configuredServices =
ServiceConfiguration.configurations.find().map(function (config) {
return config.service;
});
if (_.intersection(configuredServices, usedServices).length > 0) {
u.loginStateSignedUp = true;
}
});
}
LoginStateConstructor.prototype.loggedIn = function () {
return (!! Meteor.userId());
};
LoginStateConstructor.prototype.signedUp = function (user) {
var self = this;
if (user === undefined) {
user = Meteor.user();
}
if (!user) {
return false;
}
user.loginStateSignedUp = false;
self._signedUpHook.each(function (cb) {
cb(user);
return true;
});
return user.loginStateSignedUp;
};
LoginStateConstructor.prototype.addSignedUpInterceptor = function (cb) {
var self = this;
var stopper = self._signedUpHook.register(cb);
self._fireInterceptorsChanged();
var origStop = stopper.stop;
stopper.stop = function ( /* arguments */ ) {
origStop.apply(this, arguments);
self._fireInterceptorsChanged();
};
return stopper;
};
LoginStateConstructor.prototype._onInterceptorsChanged = function (cb) {
var self = this;
return self._interceptorsChangedHook.register(cb);
};
LoginStateConstructor.prototype._fireInterceptorsChanged = function () {
var self = this;
self._interceptorsChangedHook.each(function (cb) {
cb();
return true;
});
};
LoginState = new LoginStateConstructor();
// Add a client-only `loginStateSignedUp` property to the current user record
// and keep it updated to refect whether the user has signed up according the
// the callbacks registered with `LoginState.addSignedUpInterceptor()`.
Meteor.publish(null, function () {
var self = this;
// The function to call to notify the subscriber. We initially set it to
// self.added to workaround meteorhacks:fast-render issue #142
// (https://github.com/kadirahq/fast-render/issues/142). Once self.added() is
// called once, we set it to self.changed().
var updateFunc = self.added.bind(self);
if (!self.userId) {
return null;
}
var userObserverStopper = Meteor.users.find({
_id: self.userId
}).observeChanges({
added: updateLoginStateSignedUp,
changed: updateLoginStateSignedUp
});
var configsObserverStopper =
ServiceConfiguration.configurations.find().observe({
added: updateLoginStateSignedUp,
removed: updateLoginStateSignedUp
});
var interceptorsChangedStopper =
LoginState._onInterceptorsChanged(updateLoginStateSignedUp);
self.onStop(function () {
userObserverStopper.stop();
configsObserverStopper.stop();
interceptorsChangedStopper.stop();
});
self.ready();
function updateLoginStateSignedUp() {
var user = Meteor.users.findOne({
_id: self.userId
});
if (!user) {
// user has been removed, so no need to change.
// Also LoginState.signedUp() would call Meteor.user() if the user it is
// passed is falsey, and that would trigger an console error about
// needing to use this.userId instead.
return;
}
updateFunc('users', self.userId, {
loginStateSignedUp: LoginState.signedUp(user)
});
updateFunc = self.changed.bind(self);
}
});