Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Last active June 12, 2021 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/9b81e271232128b2fe487804ebea5923 to your computer and use it in GitHub Desktop.
Save harryWonder/9b81e271232128b2fe487804ebea5923 to your computer and use it in GitHub Desktop.
const Jwt = require('jsonwebtoken');
const CryptoJs = require('crypto-js');
const Controller = require('../Controller');
const UserModel = require('../../models/Users');
/* Validators */
const Validators = require('../../validators/Auth')
class Signup extends Controller {
constructor() { super(); }
async signup(req, res) {
try {
const Body = req.body;
const Validator = await Validators.register(Body, UserModel);
if (Validator.status) {
return super.response(res,
400,
'There are some errors in your request. Please, try again.', {},
Validator.errors);
}
/* Create The User */
let newUser = new UserModel({
firstName: Body.firstName,
lastName: Body.lastName,
email: Body.email,
status: 1,
password: CryptoJs.AES.encrypt(Body.password, super.fetchAppConfigs().appKey).toString(),
});
const User = await newUser.save();
const Token = Jwt.sign({
data: {
access: 'user-level',
phone: User.phone,
email: User.email
}
}, super.fetchAppConfigs().jwtSecretUser, { expiresIn: '3days' });
return super.response(res, 200, 'Registration Successfull', { user: User, token: Token });
} catch (e) {
return super.response(res, 500, 'An unexpected error occurred. Please, try again.', {}, { server: 'Operation Failed' });
}
}
}
module.exports = new Signup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment