Skip to content

Commit

Permalink
server-side page redirect using getServerSideProps
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Nov 5, 2021
1 parent c8b2d24 commit 1dc9230
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
25 changes: 25 additions & 0 deletions app/lib/relay/withRelayOptions.tsx
Original file line number Diff line number Diff line change
@@ -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: <div>Loading...</div>,
Expand All @@ -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;
16 changes: 1 addition & 15 deletions app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
3 changes: 0 additions & 3 deletions app/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -51,8 +50,6 @@ app.prepare().then(async () => {

server.use(graphQlMiddleware());

server.use(authorizationMiddleware());

server.get("*", async (req, res) => {
return handle(req, res);
});
Expand Down
19 changes: 0 additions & 19 deletions app/server/middleware/authorization.ts

This file was deleted.

0 comments on commit 1dc9230

Please sign in to comment.