-
-
Save aanchirinah/b2487716cbb59b75e1164ef27773edd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import crypto from 'crypto-random-string'; | |
const { sendVerificationEmail } = require('./SendGridEmailHelper'); | |
const SignUpController = (req, res, next) => { | |
return models.User.findOrCreate({ | |
where: { email: req.body.email }, | |
defaults: req.body | |
}) | |
.spread((user, created) => { | |
// if user email already exists | |
if(!created) { | |
return res.status(409).json('User with email address already exists'); | |
} else { | |
return models.VerificationToken.create({ | |
userId: user.id, | |
token: crypto(16) | |
}).then((result) => { | |
sendVerificationEmail(user.email, result.token); | |
return res.status(200).json(`${user.email} account created successfully`); | |
}) | |
.catch((error) => { | |
return res.status(500).json(error); | |
}); | |
} | |
}) | |
.catch((error) => { | |
return res.status(500).json(error); | |
}); | |
}; | |
export default SignUpController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment