Skip to content

Commit

Permalink
Require nodes as AuthWall props instead of component functions (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian authored Jan 20, 2023
1 parent c728361 commit 411aa7a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
19 changes: 18 additions & 1 deletion packages/auth/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

module.exports = {
extends: ["@recidiviz"],
plugins: ["header"],
Expand All @@ -15,5 +32,5 @@ module.exports = {
],
},
],
}
},
};
12 changes: 6 additions & 6 deletions packages/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Initialize the AuthStore by providing it with `{ authSettings }` (see [Auth0Clie
```
<AuthWall
authStore
Loading
UnauthorizedPage
EmailVerificationPage
loading
unauthorizedPage
emailVerificationPage
handleTargetUrl
>
<ProtectedComponent />
Expand All @@ -52,9 +52,9 @@ Initialize the AuthStore by providing it with `{ authSettings }` (see [Auth0Clie
| Required Props | Description |
| -: | - |
| authStore | initialized AuthStore |
| Loading | component that renders the loading page/state |
| UnauthorizedPage | component that renders the page for an unauthorized user logging in |
| EmailVerificationPage | component that renders the page for a user who has not verified their email address |
| loading | contents to render for loading page/state |
| unauthorizedPage | contents to render for an unauthorized user logging in |
| emailVerificationPage | contents to render for a user who has not verified their email address |

<br />

Expand Down
2 changes: 1 addition & 1 deletion packages/auth/license-header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/Auth.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down
6 changes: 2 additions & 4 deletions packages/auth/src/AuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -131,9 +131,7 @@ export class AuthStore {
return this.authClient?.logout({ returnTo: window.location.origin });
}

async getTokenSilently(
options?: GetTokenSilentlyOptions
): Promise<string | Error> {
async getTokenSilently(options?: GetTokenSilentlyOptions): Promise<string> {
if (this.authClient) {
try {
return this.authClient.getTokenSilently(options);
Expand Down
24 changes: 12 additions & 12 deletions packages/auth/src/AuthWall.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -17,24 +17,24 @@

import { when } from "mobx";
import { observer } from "mobx-react-lite";
import React, { useEffect } from "react";
import React, { ReactNode, useEffect } from "react";

import { AuthStore } from "./AuthStore";

type AuthWallProps = {
authStore: AuthStore;
Loading: () => JSX.Element;
UnauthorizedPage: () => JSX.Element;
EmailVerificationPage: () => JSX.Element;
loading: ReactNode;
unauthorizedPage: ReactNode;
emailVerificationPage: ReactNode;
handleTargetUrl?: (targetUrl: string) => void;
children: React.ReactNode;
children: ReactNode;
};

const AuthWall: React.FC<AuthWallProps> = ({
authStore,
Loading,
UnauthorizedPage,
EmailVerificationPage,
loading,
unauthorizedPage,
emailVerificationPage,
handleTargetUrl,
children,
}): React.ReactElement | null => {
Expand All @@ -50,15 +50,15 @@ const AuthWall: React.FC<AuthWallProps> = ({
);

if (authStore.isLoading) {
return <Loading />;
return <>{loading}</>;
}

if (!authStore.isAuthorized) {
return <UnauthorizedPage />;
return <>{unauthorizedPage}</>;
}

if (!authStore.emailVerified) {
return <EmailVerificationPage />;
return <>{emailVerificationPage}</>;
}

return authStore.isAuthorized ? <>{children}</> : null;
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
// Copyright (C) 2023 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 411aa7a

Please sign in to comment.