-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch PrivateRoute to RequireAuthentication using newer react-router…
… concepts. Signed-off-by: Michael Nelson <minelson@vmware.com>
- Loading branch information
1 parent
1d84082
commit b13c806
Showing
15 changed files
with
269 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
dashboard/src/components/PrivateRoute/PrivateRoute.test.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
75 changes: 75 additions & 0 deletions
75
dashboard/src/components/RequireAuthentication/RequireAuthentication.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2018-2022 the Kubeapps contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { createMemoryHistory } from "history"; | ||
import { Route, Router } from "react-router-dom"; | ||
import { renderWithProviders } from "shared/specs/mountWrapper"; | ||
import { RequireAuthentication } from "./RequireAuthentication"; | ||
import { screen } from "@testing-library/react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
|
||
it("redirects to the /login route if not authenticated", () => { | ||
renderWithProviders(( | ||
<Router history={createMemoryHistory()}> | ||
<RequireAuthentication> | ||
<h1>Authenticated</h1> | ||
</RequireAuthentication> | ||
<Route path="/login"> | ||
<h1>Login</h1> | ||
</Route> | ||
</Router> | ||
), { | ||
preloadedState: { | ||
auth: { | ||
authenticated: false, | ||
sessionExpired: false, | ||
} | ||
} | ||
}); | ||
|
||
expect(screen.getByRole("heading")).toHaveTextContent("Login"); | ||
}); | ||
|
||
it("renders the given component when authenticated", () => { | ||
renderWithProviders(( | ||
<Router history={createMemoryHistory()}> | ||
<RequireAuthentication> | ||
<h1>Authenticated</h1> | ||
</RequireAuthentication> | ||
<Route path="/login"> | ||
<h1>Login</h1> | ||
</Route> | ||
</Router> | ||
), { | ||
preloadedState: { | ||
auth: { | ||
authenticated: true, | ||
sessionExpired: false, | ||
} | ||
} | ||
}); | ||
|
||
expect(screen.getByRole("heading")).toHaveTextContent("Authenticated"); | ||
}); | ||
|
||
it("renders modal to reload the page if the session is expired", () => { | ||
renderWithProviders(( | ||
<Router history={createMemoryHistory()}> | ||
<RequireAuthentication> | ||
<h1>Authenticated</h1> | ||
</RequireAuthentication> | ||
<Route path="/login"> | ||
<h1>Login</h1> | ||
</Route> | ||
</Router> | ||
), { | ||
preloadedState: { | ||
auth: { | ||
authenticated: false, | ||
sessionExpired: true, | ||
} | ||
} | ||
}); | ||
|
||
expect(screen.getByRole("dialog")).toHaveTextContent("Your session has expired"); | ||
}); |
53 changes: 53 additions & 0 deletions
53
dashboard/src/components/RequireAuthentication/RequireAuthentication.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2018-2022 the Kubeapps contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { CdsButton } from "@cds/react/button"; | ||
import { CdsModal, CdsModalActions, CdsModalContent } from "@cds/react/modal"; | ||
import { ReactElement } from "react"; | ||
import { Redirect, useLocation } from "react-router-dom"; | ||
import "./RequireAuthentication.css"; | ||
import { useSelector } from "react-redux"; | ||
import { IStoreState } from "shared/types"; | ||
|
||
interface IRequireAuthenticationProps { | ||
children: ReactElement; | ||
} | ||
|
||
export function RequireAuthentication({ | ||
children, | ||
}: IRequireAuthenticationProps): ReactElement { | ||
const { authenticated, sessionExpired } = useSelector((state: IStoreState) => state.auth) | ||
const refreshPage = () => { | ||
window.location.reload(); | ||
}; | ||
const location = useLocation(); | ||
if (authenticated && children) { | ||
return children; | ||
} else { | ||
return ( | ||
<> | ||
{" "} | ||
{sessionExpired ? ( | ||
<CdsModal closable={false} onCloseChange={refreshPage}> | ||
<CdsModalContent> | ||
{" "} | ||
<p> | ||
Your session has expired or the connection has been lost, please reload the page. | ||
</p> | ||
</CdsModalContent> | ||
<CdsModalActions> | ||
<CdsButton onClick={refreshPage} type="button"> | ||
Reload | ||
</CdsButton> | ||
</CdsModalActions> | ||
</CdsModal> | ||
) : ( | ||
<Redirect to={{ pathname: "/login", state: { from: location.pathname } }} /> | ||
) | ||
} | ||
</> | ||
); | ||
} | ||
}; | ||
|
||
export default RequireAuthentication; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright 2018-2022 the Kubeapps contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import RequireAuthentication from "./RequireAuthentication"; | ||
|
||
export default RequireAuthentication; |
19 changes: 0 additions & 19 deletions
19
dashboard/src/containers/PrivateRouteContainer/PrivateRouteContainer.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.