So I decided to implement my own account management system as I accounts-ui family of packages wouldn’t quite fit in my react template without a lot of hacks.
I have defined the following files.
#server/accounts-validation.js
Accounts.validateNewUser((user) => {
console.log("validate ", user)
if (user.emails[0].address.length >= 10) {
return true;
} else {
throw new Meteor.Error(403, 'Email must have at least 10 characters');
}
});
And
imports/api/signupuser.js
import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base'
const checkEmptyString = Match.Where((x) => {
check(x, String)
return x.length > 0;
});
const checkPhone = Match.Where((x) => {
return Boolean(x.match("[0-9]*")) && x.length > 0
});
const generateUsername = (full_name) => {
let username = full_name.split(' ').join('-')
// check if username is already present. If so append an arbitrary digit
// To Be Finished
return username
}
export const SignUpUser = (full_name, email, phone, membership_type, password, c_password, accept_tc) => {
check(full_name, checkEmptyString)
check(email, checkEmptyString);
check(phone, checkPhone);
if (!Match.test(membership_type, Match.OneOf("Artist", "Sponsor"))) {
console.log("Args here")
throw new Match.Error('Member must be one of Artist or Sponsor')
}
check(password, checkEmptyString);
check(c_password, checkEmptyString);
check(accept_tc, true);
// Add more validations
if (password !== c_password) {
throw new Meteor.Error('Passwords do not match');
}
if (Meteor.isClient) {
Accounts.createUser({username: generateUsername(full_name), email: email, full_name: full_name,
phone: phone, membership_type: membership_type, password: password}, (err) => {
if (err) console.log("createUser error: ", err)
}
);
}
};
Sign up is currently working as expected. But my problem remains. If an error is thrown (eg throw new Meteor.Error(403, 'Email must have at least 10 characters')) somewhere along the chain of validating the new user (I plan to add more validations), how do I display that error on the client? I don’t mean having it in the browser console. I’m thinking I could use React states to store them and display them next to the form if they exist but I don’t know how to get the error in the client in the first place.