Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jerry - email verification admin accounts #582

Merged
merged 10 commits into from
Jan 13, 2024
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 @@ -105,6 +106,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 @@ -122,6 +124,7 @@ const userProfileController = function (UserProfile) {
},
});


if (userByEmail) {
res.status(400).send({
error:
Expand All @@ -131,6 +134,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 @@ -198,6 +229,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 @@ -847,7 +879,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 @@ -160,6 +160,8 @@ const userProfileSchema = new Schema({
areaName: { type: String },
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 },
});

userProfileSchema.pre('save', function (next) {
Expand Down
Loading