diff --git a/app/lib/relay/withRelayOptions.tsx b/app/lib/relay/withRelayOptions.tsx index ba3ab05aad..7970b042a9 100644 --- a/app/lib/relay/withRelayOptions.tsx +++ b/app/lib/relay/withRelayOptions.tsx @@ -1,4 +1,7 @@ import { getClientEnvironment } from "./client"; +import { getUserGroups } from "server/helpers/userGroupAuthentication"; +import { getUserGroupLandingRoute } from "lib/userGroups"; +import { isRouteAuthorized } from "lib/authorization"; const withRelayOptions = { fallback:
Loading...
, @@ -7,6 +10,28 @@ const withRelayOptions = { const { createServerEnvironment } = await import("./server"); return createServerEnvironment({ cookieHeader: ctx.req.headers.cookie }); }, + serverSideProps: async (ctx) => { + // Server-side redirection of the user to their landing route, if they are logged in + const groups = getUserGroups(ctx.req); + const isAuthorized = isRouteAuthorized(ctx.req.path, groups); + + if (isAuthorized) return {}; + + if (groups.length === 0) { + return { + redirect: { + destination: `/login-redirect?redirectTo=${encodeURIComponent( + ctx.req.path + )}`, + }, + }; + } + const landingRoute = getUserGroupLandingRoute(groups); + + return { + redirect: { destination: landingRoute, permanent: false }, + }; + }, }; export default withRelayOptions; diff --git a/app/pages/index.tsx b/app/pages/index.tsx index 55b75cafc1..e35efdfc80 100644 --- a/app/pages/index.tsx +++ b/app/pages/index.tsx @@ -1,8 +1,6 @@ import DefaultLayout from "components/Layout/DefaultLayout"; import { withRelay, RelayProps } from "relay-nextjs"; import { graphql, usePreloadedQuery } from "react-relay/hooks"; -import { getUserGroups } from "server/helpers/userGroupAuthentication"; -import { getUserGroupLandingRoute } from "lib/userGroups"; import { pagesQuery } from "__generated__/pagesQuery.graphql"; import withRelayOptions from "lib/relay/withRelayOptions"; @@ -28,16 +26,4 @@ function Index({ preloadedQuery }: RelayProps<{}, pagesQuery>) { ); } -export default withRelay(Index, IndexQuery, { - ...withRelayOptions, - serverSideProps: async (ctx) => { - // Server-side redirection of the user to their landing route, if they are logged in - const groups = getUserGroups(ctx.req); - const landingRoute = getUserGroupLandingRoute(groups); - if (landingRoute === "/") return {}; - - return { - redirect: { destination: landingRoute, permanent: false }, - }; - }, -}); +export default withRelay(Index, IndexQuery, withRelayOptions); diff --git a/app/server/index.ts b/app/server/index.ts index eb792a8629..7bc1ee212b 100644 --- a/app/server/index.ts +++ b/app/server/index.ts @@ -12,7 +12,6 @@ import headersMiddleware from "./middleware/headers"; import graphQlMiddleware from "./middleware/graphql"; import { pgPool } from "./db"; import ssoMiddleware from "./middleware/sso"; -import authorizationMiddleware from "./middleware/authorization"; const port = Number.parseInt(process.env.PORT, 10) || 3004; const dev = process.env.NODE_ENV !== "production"; @@ -51,8 +50,6 @@ app.prepare().then(async () => { server.use(graphQlMiddleware()); - server.use(authorizationMiddleware()); - server.get("*", async (req, res) => { return handle(req, res); }); diff --git a/app/server/middleware/authorization.ts b/app/server/middleware/authorization.ts deleted file mode 100644 index 37a950b78d..0000000000 --- a/app/server/middleware/authorization.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Request, Response } from "express"; -import { isRouteAuthorized } from "../../lib/authorization"; -import { getUserGroupLandingRoute } from "../../lib/userGroups"; -import { getUserGroups } from "../helpers/userGroupAuthentication"; - -export default function authorizationMiddleware() { - return (req: Request, res: Response, next) => { - const groups = getUserGroups(req); - if (req.path.startsWith("/_next") || isRouteAuthorized(req.path, groups)) { - next(); - } else if (groups.length === 0) { - res.redirect( - `/login-redirect?redirectTo=${encodeURIComponent(req.path)}` - ); - } else { - res.redirect(getUserGroupLandingRoute(groups)); - } - }; -}