I'm learning NodeJS coming from Laravel. I've setup a basic validation for user registration in the hook provided by the Feathers generator.
How would I use the user service to check if an email address already exists in the DB? I'm using MongoDB as the database for the service
const {authenticate} = require('@feathersjs/authentication').hooks;
const validator = require('validator');
const {
hashPassword, protect
} = require('@feathersjs/authentication-local').hooks;
module.exports = {
before: {
all: [],
find: [authenticate('jwt')],
get: [authenticate('jwt')],
create: [hashPassword(),
function(context) {
let input = context.data;
const userService = app.service('users');
if (validator.isEmpty(input.name)) {
throw new Error('Please enter your name.');
}
if (validator.isEmpty(input.email)) {
throw new Error('Please enter your email address.');
} else if (!validator.isEmail(input.email)) {
throw new Error('Please enter a valid email address.');
}
if (validator.isEmpty(input.password)) {
throw new Error('Please enter a password for your account.');
}
userService.find({
query: {
email: input.email
}
}).then((users) => {
console.log(users);
})
}
],
update: [hashPassword(), authenticate('jwt')],
patch: [hashPassword(), authenticate('jwt')],
remove: [authenticate('jwt')]
},
after: {
all: [
// Make sure the password field is never sent to the client
// Always must be the last hook
protect('password')
],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [
],
update: [],
patch: [],
remove: []
}
};