Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.register = async (req, res, next) => {
- var profilePicturePath = "";
- if(!req.files.profilePicture && !req.fields.profilePicture){
- req.files.profilePicture = '../../img/icon_default.png';
- }else if(!req.files.profilePicture && req.fields.profilePicture){
- req.files.profilePicture = '../../img/icon_default.png';
- }else{
- if(req.files.profilePicture.type != 'application/octet-stream' && req.files.profilePicture.type != 'image/jpeg' &&
- req.files.profilePicture.type != 'image/jpg' && req.files.profilePicture.type != 'image/png'){
- return next("File Uploaded for Profile is not an image.");
- }
- }
- if (req.fields.password !== req.fields.passwordConf) {
- var err = new Error('Passwords do not match.');
- err.status = 402;
- return next(err);
- }
- try {
- if (req.fields.firstname && req.fields.lastname && req.fields.username && req.fields.email
- && req.fields.password && req.fields.passwordConf && req.fields.birthDate
- && req.fields.gender) //&& req.fields.alias
- {
- let existingUsername = await User.checkUsernameAsync(req.fields.username);
- if(existingUsername != null){
- res.send({ status: 406 });
- }
- let existingEmail = await User.checkEmailAsync(req.fields.email);
- if(existingEmail != null){
- res.send({ status: 405 });
- }
- let profilePicturePath = await exports.uploaderAsync(req.files.profilePicture);
- if(!profilePicturePath){
- profilePicturePath = '../../img/icon_default.png';
- }
- var userData = new User({
- firstname: req.fields.firstname,
- surname: req.fields.lastname,
- username: req.fields.username,
- email: req.fields.email,
- password: req.fields.password,
- //passwordConf: req.fields.passwordConf,
- birthday: req.fields.birthDate,
- gender: req.fields.gender,
- //aliasImg: aliasPicturePath,
- profileImg: profilePicturePath,
- });
- userData.save()
- .then(user => {
- var token = new Token({ _userId: user._id, token: crypto.randomBytes(16).toString('hex') });
- token.save();
- return { user: user, token: token };
- })
- .then(savedToken => {
- var transporter = nodemailer.createTransport({ service: "Gmail",
- auth: {
- user: credentials.emailAddress,
- pass: credentials.password
- }
- });
- var mailOptions = { from: 'no-reply@thinknoel.com', to: savedToken.user.email, subject: 'Account Verification Token', text: 'Hello,\n\n' +
- 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/Register\/confirm?token=' + savedToken.token.token + '.\n' };
- var transRes = transporter.sendMail(mailOptions);
- return { transRes: transRes, user: savedToken.user};
- })
- .then(sentMail => {
- // console.log(sentMail);
- res.send({ status: 200, msg: 'A verification email has been sent to ' + sentMail.user.email + '.'});
- })
- .catch(err => {
- //console.log(err);
- next(err);
- });
- }else{
- res.send({ status:403, msg: 'All fields required.' });
- }
- } catch(err) {
- err => res.status(500).json({ msg: err })
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement