summaryrefslogtreecommitdiffstats
path: root/packages/meteor-useraccounts-core/lib/core.js
blob: 1c7bc07abb5880c316f3827ae9b09980e274d0c5 (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// ---------------------------------------------------------------------------------
// Patterns for methods" parameters
// ---------------------------------------------------------------------------------

STATE_PAT = {
  changePwd: Match.Optional(String),
  enrollAccount: Match.Optional(String),
  forgotPwd: Match.Optional(String),
  resetPwd: Match.Optional(String),
  signIn: Match.Optional(String),
  signUp: Match.Optional(String),
  verifyEmail: Match.Optional(String),
  resendVerificationEmail: Match.Optional(String),
};

ERRORS_PAT = {
  accountsCreationDisabled: Match.Optional(String),
  cannotRemoveService: Match.Optional(String),
  captchaVerification: Match.Optional(String),
  loginForbidden: Match.Optional(String),
  mustBeLoggedIn: Match.Optional(String),
  pwdMismatch: Match.Optional(String),
  validationErrors: Match.Optional(String),
  verifyEmailFirst: Match.Optional(String),
};

INFO_PAT = {
  emailSent: Match.Optional(String),
  emailVerified: Match.Optional(String),
  pwdChanged: Match.Optional(String),
  pwdReset: Match.Optional(String),
  pwdSet: Match.Optional(String),
  signUpVerifyEmail: Match.Optional(String),
  verificationEmailSent: Match.Optional(String),
};

INPUT_ICONS_PAT = {
  hasError: Match.Optional(String),
  hasSuccess: Match.Optional(String),
  isValidating: Match.Optional(String),
};

ObjWithStringValues = Match.Where(function (x) {
  check(x, Object);
  _.each(_.values(x), function(value) {
      check(value, String);
  });
  return true;
});

TEXTS_PAT = {
  button: Match.Optional(STATE_PAT),
  errors: Match.Optional(ERRORS_PAT),
  info: Match.Optional(INFO_PAT),
  inputIcons: Match.Optional(INPUT_ICONS_PAT),
  maxAllowedLength: Match.Optional(String),
  minRequiredLength: Match.Optional(String),
  navSignIn: Match.Optional(String),
  navSignOut: Match.Optional(String),
  optionalField: Match.Optional(String),
  pwdLink_link: Match.Optional(String),
  pwdLink_pre: Match.Optional(String),
  pwdLink_suff: Match.Optional(String),
  requiredField: Match.Optional(String),
  resendVerificationEmailLink_pre: Match.Optional(String),
  resendVerificationEmailLink_link: Match.Optional(String),
  resendVerificationEmailLink_suff: Match.Optional(String),
  sep: Match.Optional(String),
  signInLink_link: Match.Optional(String),
  signInLink_pre: Match.Optional(String),
  signInLink_suff: Match.Optional(String),
  signUpLink_link: Match.Optional(String),
  signUpLink_pre: Match.Optional(String),
  signUpLink_suff: Match.Optional(String),
  socialAdd: Match.Optional(String),
  socialConfigure: Match.Optional(String),
  socialIcons: Match.Optional(ObjWithStringValues),
  socialRemove: Match.Optional(String),
  socialSignIn: Match.Optional(String),
  socialSignUp: Match.Optional(String),
  socialWith: Match.Optional(String),
  termsAnd: Match.Optional(String),
  termsPreamble: Match.Optional(String),
  termsPrivacy: Match.Optional(String),
  termsTerms: Match.Optional(String),
  title: Match.Optional(STATE_PAT),
};

// Configuration pattern to be checked with check
CONFIG_PAT = {
  // Behaviour
  confirmPassword: Match.Optional(Boolean),
  defaultState: Match.Optional(String),
  enablePasswordChange: Match.Optional(Boolean),
  enforceEmailVerification: Match.Optional(Boolean),
  focusFirstInput: Match.Optional(Boolean),
  forbidClientAccountCreation: Match.Optional(Boolean),
  lowercaseUsername: Match.Optional(Boolean),
  overrideLoginErrors: Match.Optional(Boolean),
  sendVerificationEmail: Match.Optional(Boolean),
  socialLoginStyle: Match.Optional(Match.OneOf("popup", "redirect")),

  // Appearance
  defaultLayout: Match.Optional(String),
  hideSignInLink: Match.Optional(Boolean),
  hideSignUpLink: Match.Optional(Boolean),
  showAddRemoveServices: Match.Optional(Boolean),
  showForgotPasswordLink: Match.Optional(Boolean),
  showResendVerificationEmailLink: Match.Optional(Boolean),
  showLabels: Match.Optional(Boolean),
  showPlaceholders: Match.Optional(Boolean),

  // Client-side Validation
  continuousValidation: Match.Optional(Boolean),
  negativeFeedback: Match.Optional(Boolean),
  negativeValidation: Match.Optional(Boolean),
  positiveFeedback: Match.Optional(Boolean),
  positiveValidation: Match.Optional(Boolean),
  showValidating: Match.Optional(Boolean),

  // Privacy Policy and Terms of Use
  privacyUrl: Match.Optional(String),
  termsUrl: Match.Optional(String),

  // Redirects
  homeRoutePath: Match.Optional(String),
  redirectTimeout: Match.Optional(Number),

  // Hooks
  onLogoutHook: Match.Optional(Function),
  onSubmitHook: Match.Optional(Function),
  preSignUpHook: Match.Optional(Function),
  postSignUpHook: Match.Optional(Function),

  texts: Match.Optional(TEXTS_PAT),

  //reCaptcha config
  reCaptcha: Match.Optional({
    data_type: Match.Optional(Match.OneOf("audio", "image")),
    secretKey: Match.Optional(String),
    siteKey: Match.Optional(String),
    theme: Match.Optional(Match.OneOf("dark", "light")),
  }),

  showReCaptcha: Match.Optional(Boolean),
};


FIELD_SUB_PAT = {
  "default": Match.Optional(String),
  changePwd: Match.Optional(String),
  enrollAccount: Match.Optional(String),
  forgotPwd: Match.Optional(String),
  resetPwd: Match.Optional(String),
  signIn: Match.Optional(String),
  signUp: Match.Optional(String),
};


// Field pattern
FIELD_PAT = {
  _id: String,
  type: String,
  required: Match.Optional(Boolean),
  displayName: Match.Optional(Match.OneOf(String, Match.Where(_.isFunction), FIELD_SUB_PAT)),
  placeholder: Match.Optional(Match.OneOf(String, FIELD_SUB_PAT)),
  select: Match.Optional([{text: String, value: Match.Any}]),
  minLength: Match.Optional(Match.Integer),
  maxLength: Match.Optional(Match.Integer),
  re: Match.Optional(RegExp),
  func: Match.Optional(Match.Where(_.isFunction)),
  errStr: Match.Optional(String),

  // Client-side Validation
  continuousValidation: Match.Optional(Boolean),
  negativeFeedback: Match.Optional(Boolean),
  negativeValidation: Match.Optional(Boolean),
  positiveValidation: Match.Optional(Boolean),
  positiveFeedback: Match.Optional(Boolean),

  // Transforms
  trim: Match.Optional(Boolean),
  lowercase: Match.Optional(Boolean),
  uppercase: Match.Optional(Boolean),
  transform: Match.Optional(Match.Where(_.isFunction)),

  // Custom options
  options: Match.Optional(Object),
  template: Match.Optional(String),
};

// -----------------------------------------------------------------------------
// AccountsTemplates object
// -----------------------------------------------------------------------------

// -------------------
// Client/Server stuff
// -------------------

// Constructor
AT = function() {

};

AT.prototype.CONFIG_PAT = CONFIG_PAT;

/*
  Each field object is represented by the following properties:
    _id:         String   (required)  // A unique field"s id / name
    type:        String   (required)  // Displayed input type
    required:    Boolean  (optional)  // Specifies Whether to fail or not when field is left empty
    displayName: String   (optional)  // The field"s name to be displayed as a label above the input element
    placeholder: String   (optional)  // The placeholder text to be displayed inside the input element
    minLength:   Integer  (optional)  // Possibly specifies the minimum allowed length
    maxLength:   Integer  (optional)  // Possibly specifies the maximum allowed length
    re:          RegExp   (optional)  // Regular expression for validation
    func:        Function (optional)  // Custom function for validation
    errStr:      String   (optional)  // Error message to be displayed in case re validation fails
*/


// Allowed input types
AT.prototype.INPUT_TYPES = [
  "checkbox",
  "email",
  "hidden",
  "password",
  "radio",
  "select",
  "tel",
  "text",
  "url",
];

// Current configuration values
AT.prototype.options = {
  // Appearance
  //defaultLayout: undefined,
  showAddRemoveServices: false,
  showForgotPasswordLink: false,
  showResendVerificationEmailLink: false,
  showLabels: true,
  showPlaceholders: true,

  // Behaviour
  confirmPassword: true,
  defaultState: "signIn",
  enablePasswordChange: false,
  focusFirstInput: !Meteor.isCordova,
  forbidClientAccountCreation: false,
  lowercaseUsername: false,
  overrideLoginErrors: true,
  sendVerificationEmail: false,
  socialLoginStyle: "popup",

  // Client-side Validation
  //continuousValidation: false,
  //negativeFeedback: false,
  //negativeValidation: false,
  //positiveValidation: false,
  //positiveFeedback: false,
  //showValidating: false,

  // Privacy Policy and Terms of Use
  privacyUrl: undefined,
  termsUrl: undefined,

  // Hooks
  onSubmitHook: undefined,
};

AT.prototype.texts = {
  button: {
    changePwd: "updateYourPassword",
    //enrollAccount: "createAccount",
    enrollAccount: "signUp",
    forgotPwd: "emailResetLink",
    resetPwd: "setPassword",
    signIn: "signIn",
    signUp: "signUp",
    resendVerificationEmail: "Send email again",
  },
  errors: {
    accountsCreationDisabled: "Client side accounts creation is disabled!!!",
    cannotRemoveService: "Cannot remove the only active service!",
    captchaVerification: "Captcha verification failed!",
    loginForbidden: "error.accounts.Login forbidden",
    mustBeLoggedIn: "error.accounts.Must be logged in",
    pwdMismatch: "error.pwdsDontMatch",
    validationErrors: "Validation Errors",
    verifyEmailFirst: "Please verify your email first. Check the email and follow the link!",
  },
  navSignIn: 'signIn',
  navSignOut: 'signOut',
  info: {
    emailSent: "info.emailSent",
    emailVerified: "info.emailVerified",
    pwdChanged: "info.passwordChanged",
    pwdReset: "info.passwordReset",
    pwdSet: "Password Set",
    signUpVerifyEmail: "Successful Registration! Please check your email and follow the instructions.",
    verificationEmailSent: "A new email has been sent to you. If the email doesn't show up in your inbox, be sure to check your spam folder.",
  },
  inputIcons: {
    isValidating: "fa fa-spinner fa-spin",
    hasSuccess: "fa fa-check",
    hasError: "fa fa-times",
  },
  maxAllowedLength: "Maximum allowed length",
  minRequiredLength: "Minimum required length",
  optionalField: "optional",
  pwdLink_pre: "",
  pwdLink_link: "forgotPassword",
  pwdLink_suff: "",
  requiredField: "Required Field",
  resendVerificationEmailLink_pre: "Verification email lost?",
  resendVerificationEmailLink_link: "Send again",
  resendVerificationEmailLink_suff: "",
  sep: "OR",
  signInLink_pre: "ifYouAlreadyHaveAnAccount",
  signInLink_link: "signin",
  signInLink_suff: "",
  signUpLink_pre: "dontHaveAnAccount",
  signUpLink_link: "signUp",
  signUpLink_suff: "",
  socialAdd: "add",
  socialConfigure: "configure",
  socialIcons: {
      "meteor-developer": "fa fa-rocket"
  },
  socialRemove: "remove",
  socialSignIn: "signIn",
  socialSignUp: "signUp",
  socialWith: "with",
  termsPreamble: "clickAgree",
  termsPrivacy: "privacyPolicy",
  termsAnd: "and",
  termsTerms: "terms",
  title: {
    changePwd: "changePassword",
    enrollAccount: "createAccount",
    forgotPwd: "resetYourPassword",
    resetPwd: "resetYourPassword",
    signIn: "signIn",
    signUp: "createAccount",
    verifyEmail: "",
    resendVerificationEmail: "Send the verification email again",
  },
};

AT.prototype.SPECIAL_FIELDS = [
  "password_again",
  "username_and_email",
];

// SignIn / SignUp fields
AT.prototype._fields = [
  new Field({
    _id: "email",
    type: "email",
    required: true,
    lowercase: true,
    trim: true,
    func: function(email) {
        return !_.contains(email, '@');
    },
    errStr: 'Invalid email',
  }),
  new Field({
    _id: "password",
    type: "password",
    required: true,
    minLength: 6,
    displayName: {
        "default": "password",
        changePwd: "newPassword",
        resetPwd: "newPassword",
    },
    placeholder: {
        "default": "password",
        changePwd: "newPassword",
        resetPwd: "newPassword",
    },
  }),
];


AT.prototype._initialized = false;

// Input type validation
AT.prototype._isValidInputType = function(value) {
    return _.indexOf(this.INPUT_TYPES, value) !== -1;
};

AT.prototype.addField = function(field) {
    // Fields can be added only before initialization
    if (this._initialized) {
      throw new Error("AccountsTemplates.addField should strictly be called before AccountsTemplates.init!");
    }

    field = _.pick(field, _.keys(FIELD_PAT));
    check(field, FIELD_PAT);
    // Checks there"s currently no field called field._id
    if (_.indexOf(_.pluck(this._fields, "_id"), field._id) !== -1) {
      throw new Error("A field called " + field._id + " already exists!");
    }
    // Validates field.type
    if (!this._isValidInputType(field.type)) {
      throw new Error("field.type is not valid!");
    }
    // Checks field.minLength is strictly positive
    if (typeof field.minLength !== "undefined" && field.minLength <= 0) {
      throw new Error("field.minLength should be greater than zero!");
    }
    // Checks field.maxLength is strictly positive
    if (typeof field.maxLength !== "undefined" && field.maxLength <= 0) {
      throw new Error("field.maxLength should be greater than zero!");
    }
    // Checks field.maxLength is greater than field.minLength
    if (typeof field.minLength !== "undefined" && typeof field.minLength !== "undefined" && field.maxLength < field.minLength) {
      throw new Error("field.maxLength should be greater than field.maxLength!");
    }

    if (!(Meteor.isServer && _.contains(this.SPECIAL_FIELDS, field._id))) {
      this._fields.push(new Field(field));
    }

    return this._fields;
};

AT.prototype.addFields = function(fields) {
  var ok;

  try { // don"t bother with `typeof` - just access `length` and `catch`
    ok = fields.length > 0 && "0" in Object(fields);
  } catch (e) {
    throw new Error("field argument should be an array of valid field objects!");
  }
  if (ok) {
    _.map(fields, function(field) {
      this.addField(field);
    }, this);
  } else {
    throw new Error("field argument should be an array of valid field objects!");
  }

  return this._fields;
};

AT.prototype.configure = function(config) {
  // Configuration options can be set only before initialization
  if (this._initialized) {
    throw new Error("Configuration options must be set before AccountsTemplates.init!");
  }

  // Updates the current configuration
  check(config, CONFIG_PAT);
  var options = _.omit(config, "texts", "reCaptcha");
  this.options = _.defaults(options, this.options);

  // Possibly sets up reCaptcha options
  var reCaptcha = config.reCaptcha;
  if (reCaptcha) {
    // Updates the current button object
    this.options.reCaptcha = _.defaults(reCaptcha, this.options.reCaptcha || {});
  }

  // Possibly sets up texts...
  if (config.texts) {
    var texts = config.texts;
    var simpleTexts = _.omit(texts, "button", "errors", "info", "inputIcons", "socialIcons", "title");

    this.texts = _.defaults(simpleTexts, this.texts);

    if (texts.button) {
      // Updates the current button object
      this.texts.button = _.defaults(texts.button, this.texts.button);
    }

    if (texts.errors) {
      // Updates the current errors object
      this.texts.errors = _.defaults(texts.errors, this.texts.errors);
    }

    if (texts.info) {
      // Updates the current info object
      this.texts.info = _.defaults(texts.info, this.texts.info);
    }

    if (texts.inputIcons) {
      // Updates the current inputIcons object
      this.texts.inputIcons = _.defaults(texts.inputIcons, this.texts.inputIcons);
    }

    if (texts.socialIcons) {
      // Updates the current socialIcons object
      this.texts.socialIcons = _.defaults(texts.socialIcons, this.texts.socialIcons);
    }

    if (texts.title) {
      // Updates the current title object
      this.texts.title = _.defaults(texts.title, this.texts.title);
    }
  }
};


AT.prototype.configureRoute = function(route, options) {
  console.warn('You now need a routing package like useraccounts:iron-routing or useraccounts:flow-routing to be able to configure routes!');
};


AT.prototype.hasField = function(fieldId) {
  return !!this.getField(fieldId);
};

AT.prototype.getField = function(fieldId) {
  var field = _.filter(this._fields, function(field) {
    return field._id === fieldId;
  });

  return (field.length === 1) ? field[0] : undefined;
};

AT.prototype.getFields = function() {
    return this._fields;
};

AT.prototype.getFieldIds = function() {
    return _.pluck(this._fields, "_id");
};

AT.prototype.getRoutePath = function(route) {
    return "#";
};

AT.prototype.oauthServices = function() {
  // Extracts names of available services
  var names;

  if (Meteor.isServer) {
    names = (Accounts.oauth && Accounts.oauth.serviceNames()) || [];
  } else {
    names = (Accounts.oauth && Accounts.loginServicesConfigured() && Accounts.oauth.serviceNames()) || [];
  }
  // Extracts names of configured services
  var configuredServices = [];

  if (Accounts.loginServiceConfiguration) {
    configuredServices = _.pluck(Accounts.loginServiceConfiguration.find().fetch(), "service");
  }

  // Builds a list of objects containing service name as _id and its configuration status
  var services = _.map(names, function(name) {
    return {
      _id : name,
      configured: _.contains(configuredServices, name),
    };
  });

  // Checks whether there is a UI to configure services...
  // XXX: this only works with the accounts-ui package
  var showUnconfigured = typeof Accounts._loginButtonsSession !== "undefined";

  // Filters out unconfigured services in case they"re not to be displayed
  if (!showUnconfigured) {
    services = _.filter(services, function(service) {
      return service.configured;
    });
  }

  // Sorts services by name
  services = _.sortBy(services, function(service) {
    return service._id;
  });

  return services;
};

AT.prototype.removeField = function(fieldId) {
  // Fields can be removed only before initialization
  if (this._initialized) {
    throw new Error("AccountsTemplates.removeField should strictly be called before AccountsTemplates.init!");
  }
  // Tries to look up the field with given _id
  var index = _.indexOf(_.pluck(this._fields, "_id"), fieldId);

  if (index !== -1) {
    return this._fields.splice(index, 1)[0];
  } else if (!(Meteor.isServer && _.contains(this.SPECIAL_FIELDS, fieldId))) {
    throw new Error("A field called " + fieldId + " does not exist!");
  }
};