server_methods.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* global
  2. AccountsTemplates
  3. */
  4. "use strict";
  5. Meteor.methods({
  6. ATCreateUserServer: function(options) {
  7. if (AccountsTemplates.options.forbidClientAccountCreation) {
  8. throw new Meteor.Error(403, AccountsTemplates.texts.errors.accountsCreationDisabled);
  9. }
  10. // createUser() does more checking.
  11. check(options, Object);
  12. var allFieldIds = AccountsTemplates.getFieldIds();
  13. // Picks-up whitelisted fields for profile
  14. var profile = options.profile;
  15. profile = _.pick(profile, allFieldIds);
  16. profile = _.omit(profile, "username", "email", "password");
  17. // Validates fields" value
  18. var signupInfo = _.clone(profile);
  19. if (options.username) {
  20. signupInfo.username = options.username;
  21. if (AccountsTemplates.options.lowercaseUsername) {
  22. signupInfo.username = signupInfo.username.trim().replace(/\s+/gm, ' ');
  23. options.profile.name = signupInfo.username;
  24. signupInfo.username = signupInfo.username.toLowerCase().replace(/\s+/gm, '');
  25. options.username = signupInfo.username;
  26. }
  27. }
  28. if (options.email) {
  29. signupInfo.email = options.email;
  30. if (AccountsTemplates.options.lowercaseUsername) {
  31. signupInfo.email = signupInfo.email.toLowerCase().replace(/\s+/gm, '');
  32. options.email = signupInfo.email;
  33. }
  34. }
  35. if (options.password) {
  36. signupInfo.password = options.password;
  37. }
  38. var validationErrors = {};
  39. var someError = false;
  40. // Validates fields values
  41. _.each(AccountsTemplates.getFields(), function(field) {
  42. var fieldId = field._id;
  43. var value = signupInfo[fieldId];
  44. if (fieldId === "password") {
  45. // Can"t Pick-up password here
  46. // NOTE: at this stage the password is already encripted,
  47. // so there is no way to validate it!!!
  48. check(value, Object);
  49. return;
  50. }
  51. var validationErr = field.validate(value, "strict");
  52. if (validationErr) {
  53. validationErrors[fieldId] = validationErr;
  54. someError = true;
  55. }
  56. });
  57. if (AccountsTemplates.options.showReCaptcha) {
  58. var secretKey = null;
  59. if (AccountsTemplates.options.reCaptcha && AccountsTemplates.options.reCaptcha.secretKey) {
  60. secretKey = AccountsTemplates.options.reCaptcha.secretKey;
  61. } else {
  62. secretKey = Meteor.settings.reCaptcha.secretKey;
  63. }
  64. var apiResponse = HTTP.post("https://www.google.com/recaptcha/api/siteverify", {
  65. params: {
  66. secret: secretKey,
  67. response: options.profile.reCaptchaResponse,
  68. remoteip: this.connection.clientAddress,
  69. }
  70. }).data;
  71. if (!apiResponse.success) {
  72. throw new Meteor.Error(403, AccountsTemplates.texts.errors.captchaVerification,
  73. apiResponse['error-codes'] ? apiResponse['error-codes'].join(", ") : "Unknown Error.");
  74. }
  75. }
  76. if (someError) {
  77. throw new Meteor.Error(403, AccountsTemplates.texts.errors.validationErrors, validationErrors);
  78. }
  79. // Possibly removes the profile field
  80. if (_.isEmpty(options.profile)) {
  81. delete options.profile;
  82. }
  83. // Create user. result contains id and token.
  84. var userId = Accounts.createUser(options);
  85. // safety belt. createUser is supposed to throw on error. send 500 error
  86. // instead of sending a verification email with empty userid.
  87. if (! userId) {
  88. throw new Error("createUser failed to insert new user");
  89. }
  90. // Call postSignUpHook, if any...
  91. var postSignUpHook = AccountsTemplates.options.postSignUpHook;
  92. if (postSignUpHook) {
  93. postSignUpHook(userId, options);
  94. }
  95. // Send a email address verification email in case the context permits it
  96. // and the specific configuration flag was set to true
  97. if (options.email && AccountsTemplates.options.sendVerificationEmail) {
  98. Accounts.sendVerificationEmail(userId, options.email);
  99. }
  100. },
  101. // Resend a user's verification e-mail
  102. ATResendVerificationEmail: function (email) {
  103. check(email, String);
  104. var user = Users.findOne({ "emails.address": email });
  105. // Send the standard error back to the client if no user exist with this e-mail
  106. if (!user) {
  107. throw new Meteor.Error(403, "User not found");
  108. }
  109. try {
  110. Accounts.sendVerificationEmail(user._id);
  111. } catch (error) {
  112. // Handle error when email already verified
  113. // https://github.com/dwinston/send-verification-email-bug
  114. throw new Meteor.Error(403, "Already verified");
  115. }
  116. },
  117. });