Skip to content

Commit f027cae

Browse files
authored
Merge pull request #8 from warrant-dev/UpdateReadme
Update README
2 parents ef846e1 + 19fc4f9 commit f027cae

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

README.md

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ const Login = () => {
101101
export default Login;
102102
```
103103

104-
### `hasWarrant(objectType, objectId, relation)`
105-
`hasWarrant` is a utility function that returns a `Promise` which resolves with `true` if the user for the current session token has the warrant with the specified `objectType`, `objectId`, and `relation` and returns `false` otherwise. Use it for fine-grained conditional rendering or for specific logic within components.
104+
### `hasWarrant(warrantCheck)`
105+
`hasWarrant` is a utility function that returns a `Promise` which resolves with `true` if the user for the current session token has the specified `warrants` and returns `false` otherwise. Use it for fine-grained conditional rendering or for specific logic within components.
106106

107107
Using `hasWarrant` through the `useWarrant` hook:
108108
```jsx
@@ -116,7 +116,14 @@ const MyComponent = () => {
116116
const fetchProtectedInfo = async () => {
117117
// Only fetch protected info from server if
118118
// user can "view" the info object "protected_info".
119-
if (await hasWarrant("info", "protected_info", "viewer")) {
119+
const userIsAuthorized = await hasWarrant({
120+
warrants: [{
121+
objectType: "info",
122+
objectId: "protected_info",
123+
relation: "viewer",
124+
}]
125+
});
126+
if (userIsAuthorized) {
120127
// request protected info from server
121128
}
122129
};
@@ -143,8 +150,15 @@ class MyComponent extends React.Component {
143150
const { hasWarrant } = this.context;
144151

145152
// Only fetch protected info from server if
146-
// user can "view" the info object "protected_info".
147-
if (await hasWarrant("info", "protected_info", "view")) {
153+
// user can "view" the info object "protected_info".
154+
const userIsAuthorized = await hasWarrant({
155+
warrants: [{
156+
objectType: "info",
157+
objectId: "protected_info",
158+
relation: "viewer",
159+
}]
160+
});
161+
if (userIsAuthorized) {
148162
await fetchProtectedInfo();
149163
}
150164
};
@@ -190,9 +204,11 @@ const App = () => {
190204
exact
191205
component={ProtectedPage}
192206
options={{
193-
objectType: "myObject",
194-
objectIdParam: "id",
195-
relation: "view",
207+
warrants: [{
208+
objectType: "myObject",
209+
objectId: "id",
210+
relation: "view",
211+
}],
196212
redirectTo: "/public_route",
197213
}}
198214
/>
@@ -215,9 +231,11 @@ const MyComponent = () => {
215231
<MyPublicComponent/>
216232
{/* hides MyProtectedComponent unless the user can "view" myObject with id object.id */}
217233
<ProtectedComponent
218-
objectType="myObject"
219-
objectId={object.id}
220-
relation="view"
234+
warrants={[{
235+
objectType: "myObject",
236+
objectId: object.id,
237+
relation: "view",
238+
}]}
221239
>
222240
<MyProtectedComponent/>
223241
</ProtectedComponent>
@@ -253,9 +271,11 @@ const App = () => {
253271
can "view" the route "protected_route".
254272
*/}
255273
<Route path="/protected_route" exact component={useWarrant(ProtectedPage, {
256-
objectType: "route",
257-
objectId: "protected_route",
258-
relation: "view",
274+
warrants: [{
275+
objectType: "route",
276+
objectId: "protected_route",
277+
relation: "view",
278+
}],
259279
redirectTo: "/public_route",
260280
})}>
261281
</Switch>
@@ -278,13 +298,39 @@ const MySecretComponent = () => {
278298
// Only render MySecretComponent if the user
279299
// can "view" the component "MySecretComponent".
280300
export default withWarrant(MySecretComponent, {
281-
objectType: "component",
282-
objectId: "MySecretComponent",
283-
relation: "view",
301+
warrants: [{
302+
objectType: "component",
303+
objectId: "MySecretComponent",
304+
relation: "view",
305+
}],
284306
redirectTo: "/",
285307
});
286308
```
287309

310+
## Support for Multiple Warrants
311+
312+
`warrants` contains the list of warrants evaluted to determine if the user has access. If `warrants` contains multiple warrants, the `op` parameter is required and specifies how the list of warrants should be evaluated.
313+
314+
**anyOf** specifies that the access check request will be authorized if *any of* the warrants are matched and will not be authorized otherwise.
315+
316+
**allOf** specifies that the access check request will be authorized if *all of* the warrants are matched and will not be authorized otherwise.
317+
318+
```jsx
319+
// User is authorized if they are a 'viewer' of protected_info OR a 'viewer' of 'another_protected_info'
320+
const isAuthorized = await hasWarrant({
321+
op: "anyOf",
322+
warrants: [{
323+
objectType: "info",
324+
objectId: "protected_info",
325+
relation: "viewer",
326+
}, {
327+
objectType: "info",
328+
objectId: "another_protected_info",
329+
relation: "viewer",
330+
}]
331+
});
332+
```
333+
288334
## Notes
289335
We’ve used a random Client Key in these code examples. Be sure to replace it with your
290336
[actual Client Key](https://app.warrant.dev) to

0 commit comments

Comments
 (0)