Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(login): Redirect after login based on user roles #1004

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion react-app/src/components/Layout/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Auth } from "aws-amplify";
import * as hooks from "@/hooks";
import * as api from "@/api";
import { renderWithQueryClientAndMemoryRouter } from "@/utils/test-helpers/renderForm";
import { setMockUsername, makoStateSubmitter, AUTH_CONFIG } from "mocks";
import { setMockUsername, makoStateSubmitter, noRoleUser, AUTH_CONFIG } from "mocks";

/**
* Mock Configurations
Expand Down Expand Up @@ -187,6 +187,34 @@ describe("Layout", () => {
});
});

describe("Navigation for logged-in users", () => {
it("navigates to dashboard if user has appropriate roles", async () => {
const setupLayoutTest = async (
viewMode: ViewMode = VIEW_MODES.DESKTOP,
userData = makoStateSubmitter,
) => {
setMockUsername(userData);
mockMediaQuery(viewMode);
await renderLayout();
};
await setupLayoutTest(VIEW_MODES.DESKTOP);
expect(screen.getByText("Dashboard")).toBeInTheDocument();
});

it("navigates to home page if user doesn't have appropriate roles", async () => {
const setupLayoutTest = async (
viewMode: ViewMode = VIEW_MODES.DESKTOP,
userData = noRoleUser,
) => {
setMockUsername(userData);
mockMediaQuery(viewMode);
await renderLayout();
};
await setupLayoutTest(VIEW_MODES.DESKTOP);
expect(screen.queryByText("Dashboard")).not.toBeInTheDocument();
});
});

describe("Navigation for logged-out users", () => {
beforeEach(() => {
setMockUsername(null);
Expand Down
9 changes: 7 additions & 2 deletions react-app/src/components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import config from "@/config";
import { ScrollToTop, SimplePageContainer, UserPrompt, Banner } from "@/components";
import { isFaqPage, isProd } from "@/utils";
import MMDLAlertBanner from "@/components/Banner/MMDLSpaBanner";

import { UserRoles } from "shared-types";
/**
* Custom hook that generates a list of navigation links based on the user's status and whether the current page is the FAQ page.
*
Expand All @@ -35,7 +35,12 @@ const useGetLinks = () => {
{
name: "Dashboard",
link: "/dashboard",
condition: userObj.user && userObj.user["custom:cms-roles"],
condition:
userObj.user &&
userObj.user["custom:cms-roles"] &&
Object.values(UserRoles).some((role) =>
userObj.user["custom:cms-roles"].includes(role),
),
},
{
name: "FAQ",
Expand Down
Loading