summaryrefslogtreecommitdiffstats
path: root/packages/meteor-useraccounts-core/lib/client.js
blob: 31c9db74491049ec38891a3933a44119e37395aa (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* global
  AT: false
*/
"use strict";

// Allowed Internal (client-side) States
AT.prototype.STATES = [
  "changePwd", // Change Password
  "enrollAccount", // Account Enrollment
  "forgotPwd", // Forgot Password
  "hide", // Nothing displayed
  "resetPwd", // Reset Password
  "signIn", // Sign In
  "signUp", // Sign Up
  "verifyEmail", // Email verification
  "resendVerificationEmail", // Resend verification email
];

AT.prototype._loginType = "";

// Flag telling whether the whole form should appear disabled
AT.prototype._disabled = false;

// State validation
AT.prototype._isValidState = function(value) {
  return _.contains(this.STATES, value);
};

// Flags used to avoid clearing errors and redirecting to previous route when
// signing in/up as a results of a call to ensureSignedIn
AT.prototype.avoidRedirect = false;
AT.prototype.avoidClearError = false;

// Token to be provided for routes like reset-password and enroll-account
AT.prototype.paramToken = null;

AT.prototype.loginType = function () {
  return this._loginType;
};

AT.prototype.getparamToken = function() {
  return this.paramToken;
};

// Getter for current state
AT.prototype.getState = function() {
  return this.state.form.get("state");
};

// Getter for disabled state
AT.prototype.disabled = function() {
  return this.state.form.equals("disabled", true) ? "disabled" : undefined;
};

// Setter for disabled state
AT.prototype.setDisabled = function(value) {
  check(value, Boolean);
  return this.state.form.set("disabled", value);
};

// Setter for current state
AT.prototype.setState = function(state, callback) {
  check(state, String);

  if (!this._isValidState(state) || (this.options.forbidClientAccountCreation && state === 'signUp')) {
    throw new Meteor.Error(500, "Internal server error", "accounts-templates-core package got an invalid state value!");
  }

  this.state.form.set("state", state);
  if (!this.avoidClearError) {
    this.clearState();
  }
  this.avoidClearError = false;

  if (_.isFunction(callback)) {
    callback();
  }
};

AT.prototype.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);
};

AT.prototype.clearError = function() {
  this.state.form.set("error", null);
};

AT.prototype.clearResult = function() {
  this.state.form.set("result", null);
};

AT.prototype.clearMessage = function() {
  this.state.form.set("message", null);
};

// Initialization
AT.prototype.init = function() {
  console.warn("[AccountsTemplates] There is no more need to call AccountsTemplates.init()! Simply remove the call ;-)");
};

AT.prototype._init = function() {
  if (this._initialized) {
    return;
  }

  var usernamePresent = this.hasField("username");
  var emailPresent = this.hasField("email");

  if (usernamePresent && emailPresent) {
    this._loginType = "username_and_email";
  } else {
    this._loginType = usernamePresent ? "username" : "email";
  }

  if (this._loginType === "username_and_email") {
    // Possibly adds the field username_and_email in case
    // it was not configured
    if (!this.hasField("username_and_email")) {
      this.addField({
        _id: "username_and_email",
        type: "text",
        displayName: "usernameOrEmail",
        placeholder: "usernameOrEmail",
        required: true,
      });
    }
  }

  // Only in case password confirmation is required
  if (this.options.confirmPassword) {
    // Possibly adds the field password_again in case
    // it was not configured
    if (!this.hasField("password_again")) {
      var pwdAgain = _.clone(this.getField("password"));

      pwdAgain._id = "password_again";
      pwdAgain.displayName = {
        "default": "passwordAgain",
        changePwd: "newPasswordAgain",
        resetPwd: "newPasswordAgain",
      };
      pwdAgain.placeholder = {
        "default": "passwordAgain",
        changePwd: "newPasswordAgain",
        resetPwd: "newPasswordAgain",
      };
      this.addField(pwdAgain);
    }
  } else {
    if (this.hasField("password_again")) {
      throw new Error("AccountsTemplates: a field password_again was added but confirmPassword is set to false!");
    }
  }

  // Possibly adds the field current_password in case
  // it was not configured
  if (this.options.enablePasswordChange) {
    if (!this.hasField("current_password")) {
      this.addField({
        _id: "current_password",
        type: "password",
        displayName: "currentPassword",
        placeholder: "currentPassword",
        required: true,
      });
    }
  }

  // Ensuser the right order of special fields
  var moveFieldAfter = function(fieldName, referenceFieldName) {
    var fieldIds = AccountsTemplates.getFieldIds();
    var refFieldId = _.indexOf(fieldIds, referenceFieldName);
    // In case the reference field is not present, just return...
    if (refFieldId === -1) {
      return;
    }

    var fieldId = _.indexOf(fieldIds, fieldName);
    // In case the sought field is not present, just return...
    if (fieldId === -1) {
      return;
    }

    if (fieldId !== -1 && fieldId !== (refFieldId + 1)) {
      // removes the field
      var field = AccountsTemplates._fields.splice(fieldId, 1)[0];
      // push the field right after the reference field position
      var newFieldIds = AccountsTemplates.getFieldIds();
      var newReferenceFieldId = _.indexOf(newFieldIds, referenceFieldName);
      AccountsTemplates._fields.splice(newReferenceFieldId + 1, 0, field);
    }
  };

  // Ensuser the right order of special fields
  var moveFieldBefore = function(fieldName, referenceFieldName) {
    var fieldIds = AccountsTemplates.getFieldIds();
    var refFieldId = _.indexOf(fieldIds, referenceFieldName);
    // In case the reference field is not present, just return...
    if (refFieldId === -1) {
      return;
    }

    var fieldId = _.indexOf(fieldIds, fieldName);
    // In case the sought field is not present, just return...
    if (fieldId === -1) {
      return;
    }

    if (fieldId !== -1 && fieldId !== (refFieldId - 1)) {
      // removes the field
      var field = AccountsTemplates._fields.splice(fieldId, 1)[0];
      // push the field right after the reference field position
      var newFieldIds = AccountsTemplates.getFieldIds();
      var newReferenceFieldId = _.indexOf(newFieldIds, referenceFieldName);
      AccountsTemplates._fields.splice(newReferenceFieldId, 0, field);
    }
  };

  // The final order should be something like:
  // - username
  // - email
  // - username_and_email
  // - password
  // - password_again
  //
  // ...so lets do it in reverse order...
  moveFieldAfter("username_and_email", "username");
  moveFieldAfter("username_and_email", "email");
  moveFieldBefore("current_password", "password");
  moveFieldAfter("password", "current_password");
  moveFieldAfter("password_again", "password");


  // Sets visibility condition and validation flags for each field
  var gPositiveValidation = !!AccountsTemplates.options.positiveValidation;
  var gNegativeValidation = !!AccountsTemplates.options.negativeValidation;
  var gShowValidating = !!AccountsTemplates.options.showValidating;
  var gContinuousValidation = !!AccountsTemplates.options.continuousValidation;
  var gNegativeFeedback = !!AccountsTemplates.options.negativeFeedback;
  var gPositiveFeedback = !!AccountsTemplates.options.positiveFeedback;

  _.each(this._fields, function(field) {
    // Visibility
    switch(field._id) {
      case "current_password":
        field.visible = ["changePwd"];
        break;
      case "email":
        field.visible = ["forgotPwd", "signUp", "resendVerificationEmail"];
        if (AccountsTemplates.loginType() === "email") {
          field.visible.push("signIn");
        }
        break;
      case "password":
        field.visible = ["changePwd", "enrollAccount", "resetPwd", "signIn", "signUp"];
        break;
      case "password_again":
        field.visible = ["changePwd", "enrollAccount", "resetPwd", "signUp"];
        break;
      case "username":
        field.visible = ["signUp"];
        if (AccountsTemplates.loginType() === "username") {
          field.visible.push("signIn");
        }
        break;
      case "username_and_email":
        field.visible = [];
        if (AccountsTemplates.loginType() === "username_and_email") {
          field.visible.push("signIn");
        }
        break;
      default:
        field.visible = ["signUp"];
    }

      // Validation
      var positiveValidation = field.positiveValidation;
      if (_.isUndefined(positiveValidation)) {
        field.positiveValidation = gPositiveValidation;
      }

      var negativeValidation = field.negativeValidation;
      if (_.isUndefined(negativeValidation)) {
        field.negativeValidation = gNegativeValidation;
      }

      field.validation = field.positiveValidation || field.negativeValidation;
      if (_.isUndefined(field.continuousValidation)) {
        field.continuousValidation = gContinuousValidation;
      }

      field.continuousValidation = field.validation && field.continuousValidation;
      if (_.isUndefined(field.negativeFeedback)) {
        field.negativeFeedback = gNegativeFeedback;
      }

      if (_.isUndefined(field.positiveFeedback)) {
        field.positiveFeedback = gPositiveFeedback;
      }

      field.feedback = field.negativeFeedback || field.positiveFeedback;
      // Validating icon
      var showValidating = field.showValidating;
      if (_.isUndefined(showValidating)) {
        field.showValidating = gShowValidating;
      }

      // Custom Template
      if (field.template) {
        if (field.template in Template) {
          Template[field.template].helpers(AccountsTemplates.atInputHelpers);
        } else {
          console.warn(
            "[UserAccounts] Warning no template " + field.template + " found!"
          );
        }
      }
  });

  // Initializes reactive states
  var form = new ReactiveDict();

  form.set("disabled", false);
  form.set("state", "signIn");
  form.set("result", null);
  form.set("error", null);
  form.set("message", null);
  this.state = {
    form: form,
  };

  // Possibly subscribes to extended user data (to get the list of registered services...)
  if (this.options.showAddRemoveServices) {
      Meteor.subscribe("userRegisteredServices");
  }

  //Check that reCaptcha site keys are available and no secret keys visible
  if (this.options.showReCaptcha) {
    var atSiteKey = null;
    var atSecretKey = null;
    var settingsSiteKey = null;
    var settingsSecretKey = null;

    if (AccountsTemplates.options.reCaptcha) {
      atSiteKey = AccountsTemplates.options.reCaptcha.siteKey;
      atSecretKey = AccountsTemplates.options.reCaptcha.secretKey;
    }

    if (Meteor.settings && Meteor.settings.public && Meteor.settings.public.reCaptcha) {
      settingsSiteKey = Meteor.settings.public.reCaptcha.siteKey;
      settingsSecretKey = Meteor.settings.public.reCaptcha.secretKey;
    }

    if (atSecretKey || settingsSecretKey) {
      //erase the secret key
      if (atSecretKey) {
          AccountsTemplates.options.reCaptcha.secretKey = null;
      }

      if (settingsSecretKey) {
          Meteor.settings.public.reCaptcha.secretKey = null;
      }

      var loc = atSecretKey ? "User Accounts configuration!" : "Meteor settings!";
      throw new Meteor.Error(401, "User Accounts: DANGER - reCaptcha private key leaked to client from " + loc
      + " Provide the key in server settings ONLY.");
    }

    if (!atSiteKey && !settingsSiteKey) {
      throw new Meteor.Error(401, "User Accounts: reCaptcha site key not found! Please provide it or set showReCaptcha to false.");
    }
  }

  // Marks AccountsTemplates as initialized
  this._initialized = true;
};

AT.prototype.linkClick = function(route) {
  if (AccountsTemplates.disabled()) {
    return;
  }

  AccountsTemplates.setState(route);

  if (AccountsTemplates.options.focusFirstInput) {
    var firstVisibleInput = _.find(this.getFields(), function(f) {
      return _.contains(f.visible, route);
    });

    if (firstVisibleInput) {
      $("input#at-field-" + firstVisibleInput._id).focus();
    }
  }
};

AT.prototype.logout = function() {
  var onLogoutHook = AccountsTemplates.options.onLogoutHook;

  Meteor.logout(function() {
    if (onLogoutHook) {
      onLogoutHook();
    }
  });
};

AT.prototype.submitCallback = function(error, state, onSuccess) {
  var onSubmitHook = AccountsTemplates.options.onSubmitHook;

  if (onSubmitHook) {
    onSubmitHook(error, state);
  }

  if (error) {
    if (_.isObject(error.details)) {
      // 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 (state) {
      AccountsTemplates.setDisabled(false);
    }
  }
};

AccountsTemplates = new AT();

// Initialization
Meteor.startup(function() {
  AccountsTemplates._init();
});