forked from meteor-useraccounts/one-account
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.js
361 lines (282 loc) · 9.29 KB
/
accounts.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
function emailObjectFromService(serviceName = "", serviceData) {
switch (serviceName) {
case "facebook":
const emailObject = {
address: serviceData.email,
};
if (serviceData.email) {
emailObject.verified = true;
}
return emailObject;
case "google":
return {
address: serviceData.email,
verified: serviceData.verified_email,
};
case "github":
const primaryEmail = _.findWhere(serviceData.emails, { primary: true });
if (primaryEmail) {
return {
address: primaryEmail.email,
verified: primaryEmail.verified,
};
} else {
return {
address: serviceData.email,
};
}
case "linkedin":
return {
address: serviceData.emailAddress,
verified: true,
};
}
}
function addServiceEmailToUser(user) {
let service = "";
if (user.services && user.services.facebook) {
service = "Facebook";
} else if (user.services && user.services.google) {
service = "Google";
} else if (user.services && user.services.github) {
service = "GitHub";
} else if (user.services && user.services.linkedin) {
service = "LinkedIn"
}
const emailObject = emailObjectFromService(service.toLowerCase(), user.services[service.toLowerCase()]);
if (emailObject) {
if (!emailObject.address) {
throw new Meteor.Error(403, `The ${service} account didn't provide an email.`);
}
if (!user.emails) {
user.emails = [];
}
user.emails.push(emailObject);
}
return user;
}
const originalUpdateOrCreateUserFromExternalService = Accounts.updateOrCreateUserFromExternalService;
Accounts.updateOrCreateUserFromExternalService = function(serviceName, serviceData, options) {
if (serviceName === "password" || serviceName === "resume") {
throw new Error(`Can't use updateOrCreateUserFromExternalService with internal service ${serviceName}`);
}
if (!_.has(serviceData, "id")) {
throw new Error(`Service data for service ${serviceName} must include id`);
}
function cancelLogin(message) {
return {
error: new Meteor.Error(Accounts["LoginCancelledError"].numericError, message),
type: serviceName,
};
}
const serviceEmailObject = emailObjectFromService(serviceName, serviceData);
function addOrUpdateEmailAndCheckIfVerified(user) {
// If service didn't provide an email don't try and add it, return true
// to prevent addAndOrVerifyEmail from sending a verification email
if (!serviceEmailObject.address) {
return true;
}
const currentEmailObject = _.findWhere(user.emails, {
address: serviceEmailObject.address,
});
if (!currentEmailObject) {
Meteor.users.update(user._id, {
$push: {
emails: serviceEmailObject,
},
});
user.emails.push(serviceEmailObject);
return serviceEmailObject.verified;
}
if (currentEmailObject.verified) {
return true;
}
if (!serviceEmailObject.verified) {
return false;
}
Meteor.users.update({
_id: user._id,
"emails.address": serviceEmailObject.address,
}, {
$set: {
"emails.$.verified": true,
},
$pull: {
"services.email.verificationTokens": {
address: serviceEmailObject.address,
}
},
});
currentEmailObject.verified = true;
return true;
}
function addAndOrVerifyEmail(user) {
try {
const sendVerificationEmail = Package["accounts-password"] && (config.equals("sendVerificationEmail", "inherit") && Accounts._options.sendVerificationEmail || config.get("sendVerificationEmail"));
if (!addOrUpdateEmailAndCheckIfVerified(user) && sendVerificationEmail) {
Accounts.sendVerificationEmail(
user._id,
serviceEmailObject.address,
);
}
} catch (e) {
if (e.name !== "MongoError") throw e;
if (e.code !== 11000) throw e;
if (e.err.indexOf("emails.address") !== -1)
throw new Meteor.Error(403, `Email provided by ${serviceName} is used by another account.`);
throw e;
}
}
function addServiceToUser(user) {
// Add service id to user, original updateOrCreate... function
// will update the service data
const modifier: any = {
$set: {
[`services.${serviceName}.id`]: serviceData.id,
},
};
Meteor.users.update(user._id, modifier);
addAndOrVerifyEmail(user);
}
if (!serviceEmailObject) {
throw new Error("Unsupported service");
}
const currentUser = Meteor.user();
// Already logged in
if (currentUser) {
// Current user is already connected with this service
if (currentUser.services[serviceName]) {
// Current user is already connected with this service account
if (currentUser.services[serviceName].id === serviceData.id) {
addAndOrVerifyEmail(currentUser);
// Update service data as usual
// i.e. call original updateOrCreate... function
// Called at end of this function
}
// Current user isn't connected with this service account
else {
// Login or create new account based on service account
// i.e. call original updateOrCreate... function
// Called at end of this functionn
}
}
// Current user isn't connected with this service
else {
const selector = {
_id: { $ne: currentUser._id },
[`services.${serviceName}.id`]: serviceData.id,
};
const otherUserWithThisServiceId = Meteor.users.findOne(selector);
// Another user is connected with this service account
if (otherUserWithThisServiceId) {
addAndOrVerifyEmail(otherUserWithThisServiceId);
// Login with this service account and switch the logged in user in
// the app
// i.e. call original updateOrCreate... function
// Called at end of this function
}
// No other user is connected with this service account, lets add it to
// the current user unless there's another user with the email provided
// by the service
else {
const selector = {
_id: { $ne: currentUser._id },
"emails.address": serviceEmailObject.address,
};
const otherUserWithThisEmail = Meteor.users.findOne(selector);
// Another user have this email, add service account to that account
// and login with that account only if service provide verified email
if (otherUserWithThisEmail && serviceEmailObject.verified) {
// Add service id to user, original updateOrCreate... function
// will update the service data
const selector = {
_id: otherUserWithThisEmail._id,
[`services.${serviceName}`]: { $exists: false },
};
const modifier: any = {
$set: {
[`services.${serviceName}.id`]: serviceData.id,
},
};
const affectedRows = Meteor.users.update(selector, modifier);
// The service account was added to the current user
if (affectedRows) {
addAndOrVerifyEmail(otherUserWithThisEmail);
// Login based on service account
// i.e. call original updateOrCreate... function
// Called at end of this function
}
// The service account wasn't added to current user, abort
// User is already connected with this service
else {
// Abort login as service couldn't be added to the user,
// We don't wanna create a new user with this service
// Don't call original updateOrCreate... function
return cancelLogin(`User is already connected with a "${serviceName}" account, aborting login`);
}
}
// No other user have this email, add service account to current user
else if (!otherUserWithThisEmail) {
// Add service id to user, original updateOrCreate... function
// will update the service data
addServiceToUser(currentUser);
// Update service data as usual
// i.e. call original updateOrCreate... function
// Called at end of this function
}
}
}
}
// Login attempt
else {
const selector = {
[`services.${serviceName}.id`]: serviceData.id,
};
const user = Meteor.users.findOne(selector);
// A user is connected with this service account
if (user) {
addAndOrVerifyEmail(user);
// Update service data as usual
// i.e. call original updateOrCreate... function
// Called at end of this function
}
// No user is connected with this service account
else {
// Check if there's a user with this email which isn't connected with
// this service
const selector = {
"emails.address": serviceEmailObject.address,
[`services.${serviceName}`]: { $exists: false },
};
const otherUser = Meteor.users.findOne(selector);
// A user with this email which isn't already connected with this
// service exist, connect it with this service account
if (otherUser && serviceEmailObject.verified) {
addServiceToUser(otherUser);
}
// No user with this email that isn't already connected with
// this service account exists, a user with this email which is already
// connected with this service account might exist
else if (!otherUser) {
const result = originalUpdateOrCreateUserFromExternalService.apply(this, arguments);
if (result && result.userId) {
const user = Meteor.users.findOne(result.userId);
user && addAndOrVerifyEmail(user);
}
return result;
}
}
}
// Call original updateOrCreate... function
return originalUpdateOrCreateUserFromExternalService.apply(this, arguments);
}
Meteor.startup(function() {
const originalOnCreateUserHook = Accounts._onCreateUserHook;
Accounts._onCreateUserHook = function(options, user) {
user = addServiceEmailToUser(user);
if (originalOnCreateUserHook) {
user = originalOnCreateUserHook.call(this, options, user);
}
return user;
}
});