From 7cc358dcb7c1153bedc89730a890537014b50899 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Thu, 22 Aug 2019 15:56:04 +0100 Subject: [PATCH] Introduce env variable for webapp origin --- app/src/api/controllers/userController.ts | 11 +++++++++-- ecosystem.config.dev-template.js | 1 + ecosystem.config.template.js | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/api/controllers/userController.ts b/app/src/api/controllers/userController.ts index 9a2b1004..ecde5ee9 100644 --- a/app/src/api/controllers/userController.ts +++ b/app/src/api/controllers/userController.ts @@ -72,8 +72,7 @@ async function resetPassword(req: express.Request, res: express.Response) { if(req.body.email != undefined) { // first stage: send reset token to email address - // this relies on the API being on the same hostname as the frontend - const { origin } = new URL(req.protocol + '://' + req.headers.host); + const origin = getWebAppOrigin(); await passwordResetService.sendPasswordResetToken(req.body.email, origin); return res.status(202).send({ success: true }); @@ -96,6 +95,14 @@ async function resetPassword(req: express.Request, res: express.Response) { } } +function getWebAppOrigin() : string { + const origin = process.env.WEBAPP_ORIGIN; + if (origin == undefined) { + throw new Error('WEBAPP_ORIGIN not defined'); + } + return origin; +} + export default { createUser, getCurrentUser, diff --git a/ecosystem.config.dev-template.js b/ecosystem.config.dev-template.js index 02e13a4f..5c26ec70 100644 --- a/ecosystem.config.dev-template.js +++ b/ecosystem.config.dev-template.js @@ -25,6 +25,7 @@ module.exports = { MAIL_SERVER_PORT: 587, MAIL_SERVER_USER: "mail_username", MAIL_SERVER_PASSWORD: "longrandompassword", + WEBAPP_ORIGIN: "http://localhost:3000", } } ] diff --git a/ecosystem.config.template.js b/ecosystem.config.template.js index 695440d5..34b454d7 100644 --- a/ecosystem.config.template.js +++ b/ecosystem.config.template.js @@ -22,6 +22,7 @@ module.exports = { MAIL_SERVER_PORT: 587, MAIL_SERVER_USER: "mail_username", MAIL_SERVER_PASSWORD: "longrandompassword", + WEBAPP_ORIGIN: "https://beta.colouring.london", } } ]