-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.js
236 lines (208 loc) · 6.89 KB
/
client.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
/* global
AccountsTemplates: false,
BlazeLayout: false,
grecaptcha: false,
FlowRouter: false,
$: false
*/
'use strict';
// Previous path used for redirect after form submit
AccountsTemplates._prevPath = null;
// Possibly keeps reference to the handle for the timed out redirect
// set on some routes
AccountsTemplates.timedOutRedirect = null;
AccountsTemplates.clearState = function() {
_.each(this._fields, function(field) {
field.clearStatus();
});
var form = this.state.form;
form.set('error', null);
form.set('result', null);
form.set('message', null);
AccountsTemplates.setDisabled(false);
// Possibly clears timed out redirects
if (AccountsTemplates.timedOutRedirect !== null) {
Meteor.clearTimeout(AccountsTemplates.timedOutRedirect);
AccountsTemplates.timedOutRedirect = null;
}
};
AccountsTemplates.getparamToken = function() {
return FlowRouter.getParam('paramToken');
};
// Getter for previous route's path
AccountsTemplates.getPrevPath = function() {
return this._prevPath;
};
// Setter for previous route's path
AccountsTemplates.setPrevPath = function(newPath) {
check(newPath, String);
this._prevPath = newPath;
};
AccountsTemplates.ensureSignedIn = function(context, redirect) {
if (!Meteor.userId()) {
// if we're not already on an AT route
if (!_.contains(AccountsTemplates.knownRoutes, context.route.name)) {
AccountsTemplates.setState(AccountsTemplates.options.defaultState, function() {
var err = AccountsTemplates.texts.errors.mustBeLoggedIn;
AccountsTemplates.state.form.set("error", [err]);
});
// redirect settings
AccountsTemplates.avoidRedirect = true;
AccountsTemplates.avoidClearError = true;
AccountsTemplates.redirectToPrevPath = true;
// redirect to defined sign-in route and then redirect back
// to original route after successful sign in
var signInRouteName = AccountsTemplates.getRouteName('signIn');
if (signInRouteName) {
redirect(signInRouteName);
}
else {
throw Error('[ensureSignedIn] no signIn route configured!');
}
}
}
};
// Stores previous path on path change...
FlowRouter.triggers.exit([
function(context) {
var routeName = context.route.name;
var knownRoute = _.contains(AccountsTemplates.knownRoutes, routeName);
if (!knownRoute) {
AccountsTemplates.setPrevPath(context.path);
}
}
]);
AccountsTemplates.linkClick = function(route) {
if (AccountsTemplates.disabled()) {
return;
}
var path = AccountsTemplates.getRoutePath(route);
if (path === '#' || AccountsTemplates.avoidRedirect || path === FlowRouter.current().path) {
AccountsTemplates.setState(route);
} else {
Meteor.defer(function() {
FlowRouter.go(path);
});
}
if (AccountsTemplates.options.focusFirstInput) {
var firstVisibleInput = _.find(this.getFields(), function(f) {
return _.contains(f.visible, route);
});
if (firstVisibleInput) {
$('input#at-field-' + firstVisibleInput._id).focus();
}
}
};
AccountsTemplates.logout = function() {
var onLogoutHook = AccountsTemplates.options.onLogoutHook;
var homeRoutePath = AccountsTemplates.options.homeRoutePath;
Meteor.logout(function() {
if (onLogoutHook) {
onLogoutHook();
} else if (homeRoutePath) {
FlowRouter.redirect(homeRoutePath);
}
});
};
AccountsTemplates.postSubmitRedirect = function(route) {
if (AccountsTemplates.avoidRedirect) {
AccountsTemplates.avoidRedirect = false;
if (AccountsTemplates.redirectToPrevPath) {
FlowRouter.redirect(AccountsTemplates.getPrevPath());
}
} else {
var nextPath = AccountsTemplates.routes[route] && AccountsTemplates.routes[route].redirect;
if (nextPath) {
if (_.isFunction(nextPath)) {
nextPath();
} else {
FlowRouter.go(nextPath);
}
} else {
var previousPath = AccountsTemplates.getPrevPath();
if (previousPath && FlowRouter.current().path !== previousPath) {
FlowRouter.go(previousPath);
} else {
var homeRoutePath = AccountsTemplates.options.homeRoutePath;
if (homeRoutePath) {
FlowRouter.go(homeRoutePath);
}
}
}
}
};
AccountsTemplates.submitCallback = function(error, state, onSuccess) {
var onSubmitHook = AccountsTemplates.options.onSubmitHook;
if (onSubmitHook) {
onSubmitHook(error, state);
}
if (error) {
if (_.isObject(error.details)) {
if (error.error === 'validation-error') {
// This error is a ValidationError from the mdg:validation-error package.
// It has a well-defined error format
// Record errors that don't correspond to fields in the form
var errorsWithoutField = [];
_.each(error.details, function(fieldError) {
var field = AccountsTemplates.getField(fieldError.name);
if (field) {
// XXX in the future, this should have a way to do i18n
field.setError(fieldError.type);
} else {
errorsWithoutField.push(fieldError.type);
}
});
if (errorsWithoutField) {
AccountsTemplates.state.form.set('error', errorsWithoutField);
}
} else {
// If error.details is an object, we may try to set fields errors from it
_.each(error.details, function(error, fieldId) {
AccountsTemplates.getField(fieldId).setError(error);
});
}
} else {
var err = 'error.accounts.Unknown error';
if (error.reason) {
err = error.reason;
}
if (err.substring(0, 15) !== 'error.accounts.') {
err = 'error.accounts.' + err;
}
AccountsTemplates.state.form.set('error', [err]);
}
AccountsTemplates.setDisabled(false);
// Possibly resets reCaptcha form
if (state === 'signUp' && AccountsTemplates.options.showReCaptcha) {
grecaptcha.reset();
}
} else {
if (onSuccess) {
onSuccess();
}
if (_.contains(['enrollAccount', 'forgotPwd', 'resetPwd', 'verifyEmail'], state)) {
var redirectTimeout = AccountsTemplates.options.redirectTimeout;
if (redirectTimeout > 0) {
AccountsTemplates.timedOutRedirect = Meteor.setTimeout(function() {
AccountsTemplates.timedOutRedirect = null;
AccountsTemplates.setDisabled(false);
AccountsTemplates.postSubmitRedirect(state);
}, redirectTimeout);
}
} else if (state) {
AccountsTemplates.setDisabled(false);
AccountsTemplates.postSubmitRedirect(state);
}
}
};
// Initialization
if (FlowRouter && FlowRouter.initialize) {
// In order for ensureSignIn triggers to work,
// AccountsTemplates must be initialized before FlowRouter
// (this is now true since useraccounts:core is being executed first...)
var oldInitialize = FlowRouter.initialize;
FlowRouter.initialize = function() {
AccountsTemplates._init();
oldInitialize.apply(this, arguments);
};
}