Created
January 24, 2020 15:38
-
-
Save AshutoshSajan/28e6e4b692893488c7b997d1f7f2a07d 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
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