Skip to content

Commit

Permalink
Merge pull request #372 from USEPA/feature/update-setting-of-base-url
Browse files Browse the repository at this point in the history
Feature/update setting of base url
  • Loading branch information
courtneymyers authored Oct 9, 2023
2 parents 0d1f949 + 42c3ff2 commit 824d8ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
20 changes: 8 additions & 12 deletions app/server/app/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ const { ensureAuthenticated } = require("../middleware");
const { createJWT, jwtCookieName } = require("../utilities/createJwt");
const log = require("../utilities/logger");

const { CLIENT_URL, SERVER_URL, SAML_PUBLIC_KEY } = process.env;
const { NODE_ENV, CLIENT_URL, SERVER_URL, SAML_PUBLIC_KEY } = process.env;
const url = NODE_ENV === "development" ? CLIENT_URL : SERVER_URL;

const router = express.Router();

/**
* NOTE: SERVER_URL is needed as fallback when using sub path, e.g. /csb
*/
const baseUrl = CLIENT_URL || SERVER_URL;

router.get(
"/login",
passport.authenticate("saml", {
successRedirect: "/",
failureRedirect: "/login/fail",
session: false,
})
}),
);

router.post(
Expand Down Expand Up @@ -57,15 +53,15 @@ router.post(
* "RelayState" will be the path that the user initially tried to access
* before being sent to /login
*/
res.redirect(`${baseUrl}${body.RelayState || "/"}`);
}
res.redirect(`${url}${body.RelayState || "/"}`);
},
);

router.get("/login/fail", (req, res) => {
const logMessage = `SAML Error - Login failed.`;
log({ level: "error", message: logMessage, req });

res.redirect(`${baseUrl}/welcome?error=saml`);
res.redirect(`${url}/welcome?error=saml`);
});

router.get("/logout", ensureAuthenticated, (req, res) => {
Expand All @@ -77,7 +73,7 @@ router.get("/logout", ensureAuthenticated, (req, res) => {
const logMessage = `SAML Error - Passport logout failed - ${err}`;
log({ level: "error", message: logMessage, req });

res.redirect(`${baseUrl}/`);
res.redirect(`${url}/`);
} else {
const logMessage = `User with email '${mail}' and member of ${userGroups} groups logged out.`;
log({ level: "info", message: logMessage, req });
Expand All @@ -97,7 +93,7 @@ const logoutCallback = (req, res) => {
* or post body), redirect to below.
*/
const relayState = req.query?.RelayState || req.body?.RelayState;
res.redirect(`${baseUrl}${relayState || "/welcome?success=logout"}`);
res.redirect(`${url}${relayState || "/welcome?success=logout"}`);
};

/**
Expand Down
5 changes: 2 additions & 3 deletions app/server/app/utilities/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const log = require("./logger");

const { CLIENT_URL, SERVER_URL } = process.env;
const { NODE_ENV, CLIENT_URL, SERVER_URL } = process.env;
const url = NODE_ENV === "development" ? CLIENT_URL : SERVER_URL;

function errorHandler(err, req, res, next) {
const logMessage = typeof err.toString === "function" ? err.toString() : err;
log({ level: "error", message: logMessage, req });

const url = CLIENT_URL || SERVER_URL;

/**
* If user was trying to log in and an error occurred, return them back to
* login page to display a message.
Expand Down

0 comments on commit 824d8ce

Please sign in to comment.