Skip to content

Instantly share code, notes, and snippets.

@Sammuel09
Created January 16, 2019 12:43
Show Gist options
  • Save Sammuel09/8d672c4e38dbdc685ca1775b74cbadc0 to your computer and use it in GitHub Desktop.
Save Sammuel09/8d672c4e38dbdc685ca1775b74cbadc0 to your computer and use it in GitHub Desktop.
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;
@ebzeal
Copy link

ebzeal commented Jan 16, 2019

Thank you. I'll merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment