How do change the email in for a user? Do you use Accounts.removeEmail and Accounts.addEmail in sequence? Will addEmail warn if email address exist? If so do I add back the first email with addEmail?
You could consider changing this around a bit. Call Accounts.addEmail
first with the new email, which will give you 3 possible outcomes:
- If any other user has an email address that matches the new incoming email address, adding will fail.
- If the user you’re modifying already has a matching email address stored with their profile, it will be updated (useful if the email case changes for example).
- If the incoming email address is truly new (can’t be matched anywhere), it will be added as an additional email address in the users profile.
If outcome 3 above happens, you could then call Accounts.removeEmail
. This way you aren’t removing the email address first then having to add it back in, in-case of failure.
How do I check what happens if there’s no callback in the function Accounts.addEmail? Do I have to check if the email actually change, before and after, and/or having two emails?
Never mind. I got it. I have to use a call to the server and if exist I will get an error back.
I do this in my app with the following Meteor.method
:
'changeEmail': function(email) {
var email = email;
check(email, String);
var user = Meteor.user();
var oldemail = user.emails;
if(oldemail != null){
Accounts.removeEmail(user._id, user.emails[0].address)
}
Accounts.addEmail(user._id, email);
Accounts.sendVerificationEmail(user._id);
return email;
},
I first check if the user already has an email address, and if they do I remove it. Then I add their new one and send them a verification link.
I triied your code but the method runs two and three times (method in a shared folder).