Created
January 16, 2019 12:43
-
-
Save Sammuel09/8d672c4e38dbdc685ca1775b74cbadc0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 model from '../models'; | |
import passwordManager from '../helpers/PasswordManager'; | |
import tokenManager from '../helpers/TokenManager'; | |
const { User } = model; | |
/** | |
* @class userController | |
* @description contains the methods used to carry out operations on a user | |
*/ | |
class UserController { | |
/** | |
* @static | |
* @param {object} req - request object | |
* @param {object} res - response object | |
* @return {res} res - Response object | |
* @memberof userController | |
*/ | |
static async signupUser(req, res) { | |
try { | |
const { | |
fullName, userName, email, password, roleId | |
} = req.body; | |
const hashedPassword = await passwordManager.hashPassword(password); | |
const foundUser = await User.findOne({ where: { email } }); | |
if (foundUser) { | |
return res.status(409).send({ | |
status: 'failure', | |
message: 'Email already exists.Enter another email' | |
}); | |
} | |
const createdUser = await User.create({ | |
userName, | |
fullName, | |
email, | |
password: hashedPassword, | |
roleId, | |
authTypeId: 1 | |
}); | |
const token = await tokenManager.createToken(createdUser); | |
return res.status(201).json({ | |
status: 'success', | |
data: { | |
token, | |
message: 'Registered a new user' | |
} | |
}); | |
} catch (err) { | |
res.status(400).json({ error: err.message }); | |
} | |
} | |
} | |
export default UserController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. I'll merge