forked from ianmartorell/meteor-accounts-ui-bootstrap-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_ui.js
120 lines (108 loc) · 4.07 KB
/
accounts_ui.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
if (!Accounts.ui){
Accounts.ui = {};
}
if (!Accounts.ui._options) {
Accounts.ui._options = {
extraSignupFields: [],
requestPermissions: {},
requestOfflineToken: {},
forceApprovalPrompt: {},
forceEmailLowercase: false,
forceUsernameLowercase: false,
forcePasswordLowercase: false
};
}
Accounts.ui.navigate = function (route, hash) {
// if router is iron-router
if (window.Router && _.isFunction(Router.go)) {
Router.go(route, hash);
}
}
Accounts.ui.config = function(options) {
// validate options keys
var VALID_KEYS = ['passwordSignupFields', 'extraSignupFields', 'forceEmailLowercase', 'forceUsernameLowercase','forcePasswordLowercase',
'requestPermissions', 'requestOfflineToken', 'forceApprovalPrompt'];
_.each(_.keys(options), function(key) {
if (!_.contains(VALID_KEYS, key)){
throw new Error("Accounts.ui.config: Invalid key: " + key);
}
});
options.extraSignupFields = options.extraSignupFields || [];
// deal with `passwordSignupFields`
if (options.passwordSignupFields) {
if (_.contains([
"USERNAME_AND_EMAIL_CONFIRM",
"USERNAME_AND_EMAIL",
"USERNAME_AND_OPTIONAL_EMAIL",
"USERNAME_ONLY",
"EMAIL_ONLY"
], options.passwordSignupFields)) {
if (Accounts.ui._options.passwordSignupFields){
throw new Error("Accounts.ui.config: Can't set `passwordSignupFields` more than once");
} else {
Accounts.ui._options.passwordSignupFields = options.passwordSignupFields;
}
} else {
throw new Error("Accounts.ui.config: Invalid option for `passwordSignupFields`: " + options.passwordSignupFields);
}
}
Accounts.ui._options.forceEmailLowercase = options.forceEmailLowercase;
Accounts.ui._options.forceUsernameLowercase = options.forceUsernameLowercase;
Accounts.ui._options.forcePasswordLowercase = options.forcePasswordLowercase;
// deal with `requestPermissions`
if (options.requestPermissions) {
_.each(options.requestPermissions, function(scope, service) {
if (Accounts.ui._options.requestPermissions[service]) {
throw new Error("Accounts.ui.config: Can't set `requestPermissions` more than once for " + service);
} else if (!(scope instanceof Array)) {
throw new Error("Accounts.ui.config: Value for `requestPermissions` must be an array");
} else {
Accounts.ui._options.requestPermissions[service] = scope;
}
});
}
if (typeof options.extraSignupFields !== 'object' || !options.extraSignupFields instanceof Array) {
throw new Error("Accounts.ui.config: `extraSignupFields` must be an array.");
} else {
if (options.extraSignupFields) {
_.each(options.extraSignupFields, function(field, index) {
if (!field.fieldName || !field.fieldLabel){
throw new Error("Accounts.ui.config: `extraSignupFields` objects must have `fieldName` and `fieldLabel` attributes.");
}
if (typeof field.visible === 'undefined'){
field.visible = true;
}
Accounts.ui._options.extraSignupFields[index] = field;
});
}
}
// deal with `requestOfflineToken`
if (options.requestOfflineToken) {
_.each(options.requestOfflineToken, function (value, service) {
if (service !== 'google'){
throw new Error("Accounts.ui.config: `requestOfflineToken` only supported for Google login at the moment.");
}
if (Accounts.ui._options.requestOfflineToken[service]) {
throw new Error("Accounts.ui.config: Can't set `requestOfflineToken` more than once for " + service);
} else {
Accounts.ui._options.requestOfflineToken[service] = value;
}
});
}
// deal with `forceApprovalPrompt`
if (options.forceApprovalPrompt) {
_.each(options.forceApprovalPrompt, function (value, service) {
if (service !== 'google'){
throw new Error("Accounts.ui.config: `forceApprovalPrompt` only supported for Google login at the moment.");
}
if (Accounts.ui._options.forceApprovalPrompt[service]) {
throw new Error("Accounts.ui.config: Can't set `forceApprovalPrompt` more than once for " + service);
} else {
Accounts.ui._options.forceApprovalPrompt[service] = value;
}
});
}
};
Accounts.ui._passwordSignupFields = function() {
return Accounts.ui._options.passwordSignupFields || "EMAIL_ONLY";
};