Last active
June 12, 2021 21:34
-
-
Save harryWonder/9b81e271232128b2fe487804ebea5923 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 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