diff options
author | Eleanor Eng <eleanor.eng5@gmail.com> | 2019-11-16 16:37:36 -0500 |
---|---|---|
committer | Eleanor Eng <eleanor.eng5@gmail.com> | 2019-11-16 16:37:36 -0500 |
commit | b5111d4f4c44b845b7dd324e4332bcdc12eda0b8 (patch) | |
tree | 3c8ecc0af2accd5a49d6ff3ba1e46e7dab82858d /src/server/RouteManager.ts | |
parent | c24c16efbf798eac1003811f6c0ed98bfc65f48e (diff) | |
parent | ab285371f6fb2a4f1e64888bafbc84b602f23416 (diff) |
merge
Diffstat (limited to 'src/server/RouteManager.ts')
-rw-r--r-- | src/server/RouteManager.ts | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/server/RouteManager.ts b/src/server/RouteManager.ts index b3864e89c..3aae5734a 100644 --- a/src/server/RouteManager.ts +++ b/src/server/RouteManager.ts @@ -1,8 +1,6 @@ import RouteSubscriber from "./RouteSubscriber"; -import { RouteStore } from "./RouteStore"; import { DashUserModel } from "./authentication/models/user_model"; import * as express from 'express'; -import { Opt } from "../new_fields/Doc"; export enum Method { GET, @@ -41,23 +39,18 @@ export default class RouteManager { } /** - * Please invoke this function when adding a new route to Dash's server. - * It ensures that any requests leading to or containing user-sensitive information - * does not execute unless Passport authentication detects a user logged in. - * @param method whether or not the request is a GET or a POST - * @param handler the action to invoke, recieving a DashUserModel and, as expected, the Express.Request and Express.Response - * @param onRejection an optional callback invoked on return if no user is found to be logged in - * @param subscribers the forward slash prepended path names (reference and add to RouteStore.ts) that will all invoke the given @param handler + * + * @param initializer */ - addSupervisedRoute(initializer: RouteInitializer) { + addSupervisedRoute = (initializer: RouteInitializer): void => { const { method, subscription, onValidation, onUnauthenticated, onError } = initializer; const isRelease = this._isRelease; let supervised = async (req: express.Request, res: express.Response) => { const { user, originalUrl: target } = req; const core = { req, res, isRelease }; - const tryExecute = async (target: (args: any) => any | Promise<any>, args: any) => { + const tryExecute = async (toExecute: (args: any) => any | Promise<any>, args: any) => { try { - await target(args); + await toExecute(args); } catch (e) { if (onError) { onError({ ...core, error: e }); @@ -72,12 +65,16 @@ export default class RouteManager { req.session!.target = target; if (onUnauthenticated) { await tryExecute(onUnauthenticated, core); + if (!res.headersSent) { + res.redirect("/login"); + } } else { - res.redirect(RouteStore.login); + res.redirect("/login"); } } setTimeout(() => { if (!res.headersSent) { + console.log("Initiating fallback for ", target); const warning = `request to ${target} fell through - this is a fallback response`; res.send({ warning }); } @@ -116,6 +113,7 @@ export const STATUS = { }; export function _error(res: express.Response, message: string, error?: any) { + console.error(message); res.statusMessage = message; res.status(STATUS.EXECUTION_ERROR).send(error); } |