Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AuthHandler.js
- /*
- * Helpers
- */
- function signToken(id) {
- return jwt.sign({ id: id }, process.env.JWT_SECRET, {
- expiresIn: 86400 // expires in 24 hours
- });
- }
- function checkIfInputIsValid(eventBody) {
- if (
- !(eventBody.password &&
- eventBody.password.length >= 7)
- ) {
- return Promise.reject(new Error('Password error. Password needs to be longer than 8 characters.'));
- }
- if (
- !(eventBody.name &&
- eventBody.name.length > 5 &&
- typeof eventBody.name === 'string')
- ) return Promise.reject(new Error('Username error. Username needs to longer than 5 characters'));
- if (
- !(eventBody.email &&
- typeof eventBody.name === 'string')
- ) return Promise.reject(new Error('Email error. Email must have valid characters.'));
- return Promise.resolve();
- }
- function register(eventBody) {
- return checkIfInputIsValid(eventBody) // validate input
- .then(() =>
- User.findOne({ email: eventBody.email }) // check if user exists
- )
- .then(user =>
- user
- ? Promise.reject(new Error('User with that email exists.'))
- : bcrypt.hash(eventBody.password, 8) // hash the pass
- )
- .then(hash =>
- User.create({ name: eventBody.name, email: eventBody.email, password: hash })
- // create the new user
- )
- .then(user => ({ auth: true, token: signToken(user._id) }));
- // sign the token and send it back
- }
Add Comment
Please, Sign In to add comment