Skip to content

Instantly share code, notes, and snippets.

@AshutoshSajan
Created January 24, 2020 15:38
Show Gist options
  • Save AshutoshSajan/28e6e4b692893488c7b997d1f7f2a07d to your computer and use it in GitHub Desktop.
Save AshutoshSajan/28e6e4b692893488c7b997d1f7f2a07d to your computer and use it in GitHub Desktop.
const User = require("../models/User");
const bcrypt = require('bcrypt');
const jwtAuth = require('../utils/jwtAuth');
module.exports = {
// create/register user
registerUser: (req, res) => {
User.findOne({
email: req.body.email
}, (err, user) => {
if (err) {
res.status(500).json({
success: false,
message: "server error",
error: err
});
} else if (!user) {
User.create(req.body, (err, user) => {
if (err) {
res.status(500).json({
success: false,
message: "server error",
error: err
});
} else if (user) {
user.password = undefined;
const token = jwtAuth.createToken(user.id, process.env.JWT_SECRET);
res.status(200).json({
success: true,
message: "user created",
user,
token
});
}
})
} else if (user) {
res.status(400).json({
success: false,
message: "user alredy exist",
});
} else {
res.status(404).json({
success: false,
message: "page not found...",
});
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment