Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async createUser(user: CreateUserType, callback: ?Function): Promise {
if (!user || user.password === undefined) {
throw new AccountsError(
'Unrecognized options for create user request',
{
username: user && user.username,
email: user && user.email,
},
400,
);
}
// In case where password is an object we assume it was prevalidated and hashed
if (!user.password ||
(isString(user.password) && !validators.validatePassword(user.password))) {
throw new AccountsError('Password is required');
}
if (!validators.validateUsername(user.username) && !validators.validateEmail(user.email)) {
throw new AccountsError('Username or Email is required');
}
const hashAlgorithm = this.options.passwordHashAlgorithm;
const password = (user.password && hashAlgorithm) ?
hashPassword(user.password, hashAlgorithm) : user.password;
const userToCreate = { ...user, password };
try {
const userId = await this.transport.createUser(userToCreate);
const { onUserCreated } = this.options;
if (callback && isFunction(callback)) {
callback();
async resetPassword(token: string, newPassword: string): Promise {
if (!validators.validatePassword(newPassword)) {
throw new AccountsError('Password is invalid!');
}
const hashAlgorithm = this.options.passwordHashAlgorithm;
const password = hashAlgorithm ? hashPassword(newPassword, hashAlgorithm) : newPassword;
try {
await this.transport.resetPassword(token, password);
} catch (err) {
throw new AccountsError(err.message);
}
}