diff --git a/common/models/user.js b/common/models/user.js index 9e3beb9862..ffdf8b288a 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -10,12 +10,16 @@ import loopback from 'loopback'; import { saveUser, observeMethod } from '../../server/utils/rx.js'; import { blacklistedUsernames } from '../../server/utils/constants.js'; import { wrapHandledError } from '../../server/utils/create-handled-error.js'; +import { + getServerFullURL, + getEmailSender, + getProtocol, + getHost, + getPort +} from '../../server/utils/url-utils.js'; const debug = debugFactory('fcc:user:remote'); const BROWNIEPOINTS_TIMEOUT = [1, 'hour']; -const isDev = process.env.NODE_ENV !== 'production'; -const devHost = process.env.HOST || 'localhost'; -const emailSender = process.env.EMAIL_SENDER || 'team@freecodecamp.org'; const createEmailError = () => new Error( 'Please check to make sure the email is a valid email address.' @@ -592,11 +596,11 @@ module.exports = function(User) { const { id: loginToken } = token; const loginEmail = user.email; - + const host = getServerFullURL(); const mailOptions = { type: 'email', to: user.email, - from: emailSender, + from: getEmailSender(), subject: 'freeCodeCamp - Authentication Request!', text: renderAuthEmail({ loginEmail, @@ -697,11 +701,11 @@ module.exports = function(User) { const mailOptions = { type: 'email', to: email, - from: emailSender, + from: getEmailSender(), subject: 'freeCodeCamp - Email Update Requested', - protocol: isDev ? null : 'https', - host: isDev ? devHost : 'freecodecamp.org', - port: isDev ? null : 443, + protocol: getProtocol(), + host: getHost(), + port: getPort(), template: path.join( __dirname, '..', diff --git a/server/utils/url-utils.js b/server/utils/url-utils.js new file mode 100644 index 0000000000..df65a80fcb --- /dev/null +++ b/server/utils/url-utils.js @@ -0,0 +1,37 @@ +const isDev = process.env.NODE_ENV !== 'production'; +const isBeta = !!process.env.BETA; + +export function getEmailSender() { + return process.env.EMAIL_SENDER || 'team@freecodecamp.com'; +} + +export function getPort() { + if (!isDev) { + return '443'; + } + return process.env.SYNC_PORT || '3000'; +} + +export function getProtocol() { + return isDev ? 'http' : 'https'; +} + +export function getHost() { + if (isDev) { + return process.env.HOST || 'localhost'; + } + return isBeta ? 'beta.freecodecamp.com' : 'freecodecamp.com'; +} + +export function getServerFullURL() { + if (!isDev) { + return getProtocol() + + '://' + + getHost(); + } + return getProtocol() + + '://' + + getHost() + + ':' + + getPort(); +}