Skip to content

Instantly share code, notes, and snippets.

@aanchirinah
Last active July 28, 2018 08:52
Show Gist options
  • Save aanchirinah/b2487716cbb59b75e1164ef27773edd9 to your computer and use it in GitHub Desktop.
Save aanchirinah/b2487716cbb59b75e1164ef27773edd9 to your computer and use it in GitHub Desktop.
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