Skip to content

Commit

Permalink
Merge pull request #582 from OneCommunityGlobal/jerry_email_verificat…
Browse files Browse the repository at this point in the history
…ion_admin_accounts

Jerry - email verification admin accounts
  • Loading branch information
one-community authored Jan 13, 2024
2 parents 0e0b411 + 497800e commit ad98001
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const moment = require('moment-timezone');

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const fetch = require("node-fetch");

const moment_ = require('moment');
const jwt = require('jsonwebtoken');
Expand Down Expand Up @@ -111,6 +112,7 @@ const userProfileController = function (UserProfile) {
};

const postUserProfile = async function (req, res) {

if (!await hasPermission(req.body.requestor, 'postUserProfile')) {
res.status(403).send('You are not authorized to create new users');
return;
Expand All @@ -128,6 +130,7 @@ const userProfileController = function (UserProfile) {
},
});


if (userByEmail) {
res.status(400).send({
error:
Expand All @@ -137,6 +140,34 @@ const userProfileController = function (UserProfile) {
return;
}

// In dev environment, if newly created user is Owner or Administrator, make fetch request to Beta login route with actualEmail and actual Password
if (process.env.dbName === 'hgnData_dev') {
if (req.body.role === 'Owner' || req.body.role === 'Administrator') {
const email = req.body.actualEmail
const password = req.body.actualPassword
const url = "https://hgn-rest-beta.azurewebsites.net/api/"
try {
// Log in to Beta login route using provided credentials
const response = await fetch(url + 'login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
if (!response.ok) {
throw new Error('Invalid credentials');
}
} catch (error) {
res.status(400).send({
error: 'The actual email or password you provided is incorrect. Please enter the actual email and password associated with your account in the Main HGN app.',
type: 'credentials',
});
return;
}
}
}

/** *
* Turn on and off the duplicate phone number checker by changing
* the value of duplicatePhoneNumberCheck variable.
Expand Down Expand Up @@ -204,6 +235,7 @@ const userProfileController = function (UserProfile) {
up.permissions = req.body.permissions;
up.bioPosted = req.body.bioPosted || 'default';
up.isFirstTimelog = true;
up.actualEmail = req.body.actualEmail;

up.save()
.then(() => {
Expand Down Expand Up @@ -853,7 +885,7 @@ const userProfileController = function (UserProfile) {
const currentRefreshToken = jwt.sign(jwtPayload, JWT_SECRET);
res.status(200).send({ refreshToken: currentRefreshToken });
};

return {
postUserProfile,
getUserProfiles,
Expand Down
2 changes: 2 additions & 0 deletions src/models/userProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ const userProfileSchema = new Schema({
areaContent: { type: String },
},
],
// actualEmail field represents the actual email associated with a real volunteer in the main HGN app. actualEmail is required for Administrator and Owner accounts only in the dev environment.
actualEmail: { type: String },
timeOffFrom: { type: Date, default: undefined },
timeOffTill: { type: Date, default: undefined },
});
Expand Down

0 comments on commit ad98001

Please sign in to comment.