Skip to content

Commit 3e51f76

Browse files
authored
Merge pull request #6 from warrant-dev/SupportV2Authorize
Support v2 authorize
2 parents a8fb6f9 + bf2e1f0 commit 3e51f76

9 files changed

+40
-69
lines changed

README.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export default MyComponent;
173173
import React from "react";
174174
import { Router, Route, Switch } from "react-router-dom";
175175
import { createBrowserHistory } from "history";
176-
import { WarrantProvider, ProtectedRoute, WARRANT_IGNORE_ID } from "@warrantdev/react-warrant-js";
176+
import { WarrantProvider, ProtectedRoute } from "@warrantdev/react-warrant-js";
177177
import PublicPage from "./PublicPage";
178178
import ProtectedPage from "./ProtectedPage";
179179

@@ -227,28 +227,6 @@ const MyComponent = () => {
227227
export default MyComponent;
228228
```
229229

230-
**NOTE:** To ignore the `objectId` when using any of the provided components, you can pass the constant `WARRANT_IGNORE_ID`. You must have a corresponding warrant that grants access to **ANY** user on the given `objectType` for this check to succeed.
231-
```jsx
232-
import React from "react";
233-
import { ProtectedComponent, WARRANT_IGNORE_ID } from "@warrantdev/react-warrant-js";
234-
235-
const MyComponent = () => {
236-
return <div>
237-
<MyPublicComponent/>
238-
{/* Only shows MyProtectedComponent if the user can "view" ANY myObject */}
239-
<ProtectedComponent
240-
objectType="myObject"
241-
objectId={WARRANT_IGNORE_ID}
242-
relation="view"
243-
>
244-
<MyProtectedComponent/>
245-
</ProtectedComponent>
246-
</div>;
247-
};
248-
249-
export default MyComponent;
250-
```
251-
252230
### `withWarrant`
253231
Use the `withWarrant` Higher Order Component (HOC) to protect components that should only be accessible to users with certain privileges.
254232

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
"react-router-dom": "^5.2.0"
4949
},
5050
"dependencies": {
51-
"@warrantdev/warrant-js": "^0.0.5"
51+
"@warrantdev/warrant-js": "^1.0.0"
5252
}
5353
}

src/ProtectedComponent.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import React, { useEffect, useState } from "react";
2+
import { WarrantCheck } from "@warrantdev/warrant-js";
23
import useWarrant from "./useWarrant";
34

4-
export interface ProtectedComponentProps {
5-
objectType: string;
6-
objectId: string;
7-
relation: string;
5+
export interface ProtectedComponentProps extends WarrantCheck {
86
children: React.ReactChildren;
97
}
108

11-
const ProtectedComponent: React.FunctionComponent<ProtectedComponentProps> = ({ objectType, objectId, relation, children }) => {
9+
const ProtectedComponent: React.FunctionComponent<ProtectedComponentProps> = ({ op, warrants, children }) => {
1210
const [showChildren, setShowChildren] = useState<boolean>(false);
1311
const { sessionToken, hasWarrant } = useWarrant();
1412

1513
useEffect(() => {
16-
if (!objectId) {
17-
throw new Error("Invalid or no objectId provided to ProtectedComponent");
14+
if (!warrants || warrants.length === 0) {
15+
throw new Error("Invalid or no warrants provided to ProtectedComponent");
1816
}
1917

2018
const checkWarrant = async () => {
21-
setShowChildren(await hasWarrant(objectType, objectId, relation));
19+
setShowChildren(await hasWarrant({ op, warrants }));
2220
}
2321

2422
if (sessionToken) {

src/ProtectedRoute.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import React, { useEffect, useState } from "react";
22
import { Redirect, Route, RouteProps } from "react-router-dom";
3-
import { WARRANT_IGNORE_ID } from "@warrantdev/warrant-js";
3+
import { WarrantCheck } from "@warrantdev/warrant-js";
44
import useWarrant from "./useWarrant";
55

6-
export interface ProtectedRouteOptions {
7-
objectType: string;
8-
objectIdParam: string;
9-
relation: string;
6+
export interface ProtectedRouteOptions extends WarrantCheck {
107
redirectTo: string;
118
}
129

@@ -17,9 +14,8 @@ export interface ProtectedRouteProps extends RouteProps {
1714

1815
const ProtectedRoute: React.FunctionComponent<ProtectedRouteProps> = ({
1916
options: {
20-
objectType,
21-
objectIdParam,
22-
relation,
17+
op,
18+
warrants,
2319
redirectTo,
2420
},
2521
component,
@@ -30,24 +26,23 @@ const ProtectedRoute: React.FunctionComponent<ProtectedRouteProps> = ({
3026
const [showWrappedComponent, setShowWrappedComponent] = useState<boolean | null>(null);
3127

3228
useEffect(() => {
33-
const checkForWarrant = async (objectId: string) => {
34-
setShowWrappedComponent(await hasWarrant(objectType, objectId, relation));
29+
const checkForWarrant = async (warrantCheck: WarrantCheck) => {
30+
setShowWrappedComponent(await hasWarrant(warrantCheck));
3531
};
3632

3733
if (sessionToken) {
38-
let objectId = "";
39-
if (objectIdParam === WARRANT_IGNORE_ID) {
40-
objectId = WARRANT_IGNORE_ID;
41-
} else {
42-
/** @ts-ignore */
43-
objectId = computedMatch.params[objectIdParam];
44-
}
34+
warrants.forEach((warrant) => {
35+
if (computedMatch.params[warrant.objectId]) {
36+
/** @ts-ignore */
37+
warrant.objectId = computedMatch.params[warrant.objectId];
38+
}
4539

46-
if (!objectId) {
47-
throw new Error("Invalid or no objectIdParam provided for ProtectedRoute");
48-
}
40+
if (!warrant.objectId) {
41+
throw new Error("Invalid or no objectId provided for ProtectedRoute");
42+
}
43+
})
4944

50-
checkForWarrant(objectId);
45+
checkForWarrant({ op, warrants });
5146
}
5247
}, [sessionToken]);
5348

src/WarrantContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { createContext } from "react";
2+
import { WarrantCheck } from "@warrantdev/warrant-js";
23

34
export interface AuthorizationContext {
45
clientKey: string;
56
sessionToken: string;
67
setSessionToken: (sessionToken: string) => void;
7-
hasWarrant: (objectType: string, objectId: string, relation: string) => Promise<boolean>;
8+
hasWarrant: (warrantCheck: WarrantCheck) => Promise<boolean>;
89
isLoading: boolean;
910
}
1011

src/WarrantProvider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useCallback, useEffect, useState } from "react";
22
import { Client as WarrantClient } from "@warrantdev/warrant-js";
33

4+
import { WarrantCheck } from "@warrantdev/warrant-js";
45
import WarrantContext from "./WarrantContext";
56

67
export interface AuthorizationProvider {
@@ -28,13 +29,13 @@ const WarrantProvider = (options: AuthorizationProvider): JSX.Element => {
2829
localStorage.setItem(LOCAL_STORAGE_KEY_SESSION_TOKEN, newSessionToken);
2930
};
3031

31-
const hasWarrant = useCallback(async (objectType: string, objectId: string, relation: string): Promise<boolean> => {
32+
const hasWarrant = useCallback(async (warrantCheck: WarrantCheck): Promise<boolean> => {
3233
if (!sessionToken) {
3334
throw new Error("No session token provided to Warrant. You may have forgotten to call setSessionToken with a valid session token to finish initializing Warrant.");
3435
}
3536

3637
setIsLoading(true);
37-
const isAuthorized = await new WarrantClient(clientKey, sessionToken).isAuthorized(objectType, objectId, relation);
38+
const isAuthorized = await new WarrantClient(clientKey, sessionToken).isAuthorized(warrantCheck);
3839
setIsLoading(false);
3940

4041
return isAuthorized;

src/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export { default as WarrantProvider } from "./WarrantProvider";
44
export { default as useWarrant } from "./useWarrant";
55
export { default as ProtectedComponent } from "./ProtectedComponent";
66
export { default as ProtectedRoute } from "./ProtectedRoute";
7-
export { WARRANT_IGNORE_ID } from "@warrantdev/warrant-js";

src/withWarrant.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React, { useEffect, useState } from "react";
2+
import { WarrantCheck } from "@warrantdev/warrant-js";
23
import useWarrant from "./useWarrant";
34

45
export interface WithWarrantOptions {
5-
objectType: string;
6-
objectId: string;
7-
relation: string;
6+
warrantCheck: WarrantCheck;
87
redirectTo: string;
98
}
109

@@ -17,13 +16,13 @@ export interface WithWarrantOptions {
1716
*/
1817
const withWarrant = (WrappedComponent: React.ComponentClass, options: WithWarrantOptions) => {
1918
return (props: any) => {
20-
const { objectType, objectId, relation, redirectTo } = options;
19+
const { warrantCheck, redirectTo } = options;
2120
const { sessionToken, hasWarrant } = useWarrant();
2221
const [showWrappedComponent, setShowWrappedComponent] = useState<boolean>(false);
2322

2423
useEffect(() => {
2524
const checkWarrant = async () => {
26-
setShowWrappedComponent(await hasWarrant(objectType, objectId, relation));
25+
setShowWrappedComponent(await hasWarrant(warrantCheck));
2726
};
2827

2928
if (sessionToken) {

0 commit comments

Comments
 (0)