You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63-17Lines changed: 63 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,8 +101,8 @@ const Login = () => {
101
101
exportdefaultLogin;
102
102
```
103
103
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.
106
106
107
107
Using `hasWarrant` through the `useWarrant` hook:
108
108
```jsx
@@ -116,7 +116,14 @@ const MyComponent = () => {
116
116
constfetchProtectedInfo=async () => {
117
117
// Only fetch protected info from server if
118
118
// user can "view" the info object "protected_info".
119
-
if (awaithasWarrant("info", "protected_info", "viewer")) {
119
+
constuserIsAuthorized=awaithasWarrant({
120
+
warrants: [{
121
+
objectType:"info",
122
+
objectId:"protected_info",
123
+
relation:"viewer",
124
+
}]
125
+
});
126
+
if (userIsAuthorized) {
120
127
// request protected info from server
121
128
}
122
129
};
@@ -143,8 +150,15 @@ class MyComponent extends React.Component {
143
150
const { hasWarrant } =this.context;
144
151
145
152
// Only fetch protected info from server if
146
-
// user can "view" the info object "protected_info".
147
-
if (awaithasWarrant("info", "protected_info", "view")) {
153
+
// user can "view" the info object "protected_info".
154
+
constuserIsAuthorized=awaithasWarrant({
155
+
warrants: [{
156
+
objectType:"info",
157
+
objectId:"protected_info",
158
+
relation:"viewer",
159
+
}]
160
+
});
161
+
if (userIsAuthorized) {
148
162
awaitfetchProtectedInfo();
149
163
}
150
164
};
@@ -190,9 +204,11 @@ const App = () => {
190
204
exact
191
205
component={ProtectedPage}
192
206
options={{
193
-
objectType:"myObject",
194
-
objectIdParam:"id",
195
-
relation:"view",
207
+
warrants: [{
208
+
objectType:"myObject",
209
+
objectId:"id",
210
+
relation:"view",
211
+
}],
196
212
redirectTo:"/public_route",
197
213
}}
198
214
/>
@@ -215,9 +231,11 @@ const MyComponent = () => {
215
231
<MyPublicComponent/>
216
232
{/* hides MyProtectedComponent unless the user can "view" myObject with id object.id */}
`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
+
constisAuthorized=awaithasWarrant({
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
+
288
334
## Notes
289
335
We’ve used a random Client Key in these code examples. Be sure to replace it with your
0 commit comments