Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5671 from RocketChat/add-rest-api-registration
Add users.register and users.getAvatar
- Loading branch information
Showing
3 changed files
with
88 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Meteor.methods({ | ||
registerUser(formData) { | ||
check(formData, Match.ObjectIncluding({ | ||
email: String, | ||
pass: String, | ||
name: String, | ||
secretURL: Match.Optional(String) | ||
})); | ||
|
||
if (RocketChat.settings.get('Accounts_RegistrationForm') === 'Disabled') { | ||
throw new Meteor.Error('error-user-registration-disabled', 'User registration is disabled', { method: 'registerUser' }); | ||
} else if (RocketChat.settings.get('Accounts_RegistrationForm') === 'Secret URL' && (!formData.secretURL || formData.secretURL !== RocketChat.settings.get('Accounts_RegistrationForm_SecretURL'))) { | ||
throw new Meteor.Error ('error-user-registration-secret', 'User registration is only allowed via Secret URL', { method: 'registerUser' }); | ||
} | ||
|
||
RocketChat.validateEmailDomain(formData.email); | ||
|
||
const userData = { | ||
email: s.trim(formData.email.toLowerCase()), | ||
password: formData.pass | ||
}; | ||
|
||
// Check if user has already been imported and never logged in. If so, set password and let it through | ||
const importedUser = RocketChat.models.Users.findOneByEmailAddress(s.trim(formData.email.toLowerCase())); | ||
let userId; | ||
if (importedUser && importedUser.importIds && importedUser.importIds.length && !importedUser.lastLogin) { | ||
Accounts.setPassword(importedUser._id, userData.password); | ||
userId = importedUser._id; | ||
} else { | ||
userId = Accounts.createUser(userData); | ||
} | ||
|
||
RocketChat.models.Users.setName(userId, s.trim(formData.name)); | ||
|
||
RocketChat.saveCustomFields(userId, formData); | ||
|
||
try { | ||
if (RocketChat.settings.get('Verification_Customized')) { | ||
const subject = RocketChat.placeholders.replace(RocketChat.settings.get('Verification_Email_Subject') || ''); | ||
const html = RocketChat.placeholders.replace(RocketChat.settings.get('Verification_Email') || ''); | ||
Accounts.emailTemplates.verifyEmail.subject = () => subject; | ||
Accounts.emailTemplates.verifyEmail.html = (userModel, url) => html.replace(/\[Verification_Url]/g, url); | ||
} | ||
|
||
Accounts.sendVerificationEmail(userId, userData.email); | ||
} catch (error) { | ||
// throw new Meteor.Error 'error-email-send-failed', 'Error trying to send email: ' + error.message, { method: 'registerUser', message: error.message } | ||
} | ||
|
||
return userId; | ||
} | ||
}); |