-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathaccounts-guest-server.js
133 lines (118 loc) · 4.35 KB
/
accounts-guest-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
Moniker = Npm.require('moniker');
Accounts.removeOldGuests = function (before) {
if (typeof before === 'undefined') {
before = new Date();
before.setHours(before.getHours() - 1);
}
res = Meteor.users.remove({createdAt: {$lte: before}, 'profile.guest': true});
return res;
};
Accounts.registerLoginHandler("guest", function (options) {
if (AccountsGuest.enabled === false || !options || !options.createGuest || Meteor.userId())
return undefined;
var newUserId = null;
if (AccountsGuest.anonymous) {
if (options.email) {
throw new Error("Can't create a guest user with an email with AccountsGuest.anonymous == true.\n");
}
newUserId = Accounts.insertUserDoc(options, {profile: {guest: true}});
} else if (!Accounts.createUser) {
throw new Error("Can't create a guest user with falsey AccountsGuest.anonymous unless the accounts-password package is installed.\n" +
"Either set AccountsGuest.anonymous or run 'meteor add accounts-password'.");
} else {
var guestOptions = createGuestOptions(options.email);
newUserId = Accounts.createUser(guestOptions);
}
return {
userId: newUserId
};
});
LoginState.addSignedUpInterceptor(function (user) {
if (user.profile && user.profile.guest && AccountsGuest.name === false) {
user.loginStateSignedUp = false;
}
});
/**
* set profile.guest to false when guest adds a service
*
*/
var bamPkg = Package['brettle:accounts-multiple'];
if (bamPkg) {
bamPkg.AccountsMultiple.register({
// brettle:accounts-add-service will cause onSwitchFailure to be called
// when a service is added.
// The new service will have been added to the attempting user.
// In that case, we want to update profile.guest.
onSwitchFailure: function (attemptingUser, attempt) {
if (attemptingUser.profile && attemptingUser.profile.guest) {
// Hide profile.guest so it doesn't effect LoginState.signedUp()
delete attemptingUser.profile.guest;
var signedUp = LoginState.signedUp(attemptingUser);
attemptingUser.profile.guest = (! signedUp);
Meteor.users.update(attemptingUser._id, {
$set: {
"profile.guest": attemptingUser.profile.guest
}
});
}
}
});
}
/**
* set profile.guest: drop guest user when visitor logs in as another user
*
*/
GuestUsers = new Mongo.Collection('guestUsers');
Accounts.onLogin(function(par){
if(par.user && par.user.username !== undefined && par.user.username.indexOf('guest') !== -1){
if(!GuestUsers.findOne({connection_id: par.connection.id})){
GuestUsers.insert({connection_id: par.connection.id, user_id: par.user._id});
}
}
else if(par.type !== 'resume'){
var guest = GuestUsers.findOne({connection_id: par.connection.id});
if (guest) {
Meteor.users.remove(guest.user_id);
GuestUsers.remove(guest._id);
}
}
});
/* adapted from pull-request https://github.com/dcsan
* See https://github.com/artwells/meteor-accounts-guest/commit/28cbbf0eca2d80f78925ac619abf53d0769c0d9d
*/
Meteor.methods({
createGuest: function (email)
{
var guest = createGuestOptions(email);
Accounts.createUser(guest);
// console.log("createGuest" + guestname);
return guest;
}
});
function createGuestOptions(email) {
check(email, Match.OneOf(String, null, undefined));
/* if explicitly disabled, happily do nothing */
if (AccountsGuest.enabled === false){
return true;
}
// count = Meteor.users.find().count() + 1
if (AccountsGuest.name === true) {
guestname = Moniker.choose();
// Just in case, let's make sure this name isn't taken
while (Meteor.users.find({username:guestname}).count() > 0) {
guestname = Moniker.choose();
}
} else {
guestname = "guest-#" + Random.id();
}
if (!email) {
email = guestname + "@example.com";
}
guest = {
username: guestname,
email: email,
profile: {guest: true},
password: Meteor.uuid(),
};
return guest;
}