On our service, Users need to provide the invitation code when registration. The registration will be confirmed on server side to check whether the invitation code matches the email to allow the registration.
I notice that Accounts.createUser() with callback is not supported on the server yet.
if (Meteor.isServer) {
Meteor.methods({
register(email, password, code) {
// check the invitation code here first
...
return new Promise((resolve, reject) => {
Accounts.createUser({
email, password
}, err => { ///////////////// raise the error
console.log('register error:', err)
if (err) reject(err);
else resolve();
});
});
}
})
}
Accounts.createUser() also doesn’t return a Promise for Meteor.methods/Meteor.call. How do I register a user on server side with Meteor.methods/Meteor.call?
Did you mean import { Accounts } from 'meteor/accounts-base'? I can’t find AccountsCommon in ‘meteor/accounts-base’.
I tried the following code, but still can’t register in the client side.
import { Accounts } from 'meteor/accounts-base';
Accounts.config({
forbidClientAccountCreation: false,
});
I also tried to import the above config on the server side, it shows an Error Can't setforbidClientAccountCreationmore than once event if I use if (Meteor.isServer) { /* config setup here */ }.
fyi, on try/catch see this below. I used to use this pattern often, but found that the NodeJS v8 engine is unable to optimize code within try/catch blocks. Within reason of course, probably will not notice it for normal operations, I just switched to different patterns to not work against this.
This aside, the requirement to try-catch at runtime demands significant hoop-jumping in v8 (Node.js JavaScript engine). Use of try-catch causes runtime de-optimizations resulting in unnecessarily slow execution – particularly for hot code paths. This means if we adopt throwing and try/catch all over a codebase it could significantly affect performance.
Generator functions
Functions that contain a for-of statement
Functions that contain a try-catch statement
Functions that contain a try-finally statement
Functions that contain a compound let assignment
Functions that contain a compound const assignment
Functions that contain object literals that contain proto, or get or set declarations.
It should work fine. Are you correctly putting all your code inside an imports folder so it doesn’t automatically get executed? If not, maybe try running Accounts.config inside a Meteor.startup callback. (server side, of course)
Yes, it doesn’t have any issues, and basically just works. It’s probably a non issue at smaller scale. The fact is that NodeJS/V8 can’t optimize any code that is within a try/catch. It is a black and white statement from NodeJS and is the only fact. So although it works/is convenient, it’s just a learned development behavior. Replacing it with a function/process that doesn’t have a built in performance limitation/penalty may be worth checking for the long run. That’s how we handled it.
Your example seems incomplete and Accounts.createUser returns the created objects key _id, so…
let _id = Accounts.createUser({username: string, email: string, password: string}, [callback]);
So you could return the value of _id on success or just use the callback.
Also I dont know why anyone would want to allow account creation from the client