Skip to content

Commit

Permalink
v0.0.3: Update client to make requests using new API endpoint/paramet…
Browse files Browse the repository at this point in the history
…ers (#2)

* v0.0.3: Update client to make requests using new API endpoint/parameters

* Update README
  • Loading branch information
kkajla12 authored Jul 18, 2021
1 parent 5191a6c commit 0ca90e4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ npm install @warrantdev/warrant-js
## Usage
Import the Warrant client and pass your Client Key to the constructor to get started:
```js
import {Client as Warrant} from "@warrantdev/warrant-js";
import { Client as Warrant } from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new Warrant('client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=', sessionToken);
```

### `isAuthorized(permissionId)`
### `isAuthorized(objectType, objectId, relation)`

This function returns a `Promise` that resolves with `true` if the user for the current session token has the permission with the specified `permissionId` and `false` otherwise.
This function returns a `Promise` that resolves with `true` if the user for the current session token has the specified `relation` on the object with id `objectId` of type `objectType` and `false` otherwise.

```js
import {Client as WarrantClient} from "@warrantdev/warrant-js";
import { Client as WarrantClient } from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new WarrantClient('client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=', sessionToken);
Expand All @@ -34,7 +34,7 @@ const warrant = new WarrantClient('client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICb
// An e-commerce website where Store Owners can edit their own Store's info
//
warrant
.isAuthorized("edit_stores")
.isAuthorized("store", storeId, "edit")
.then((isAuthorized) => {
if (isAuthorized) {
// Carry out logic to allow user to edit a Store
Expand All @@ -43,7 +43,7 @@ warrant
```
Or using async/await:
```js
import {Client as WarrantClient} from "@warrantdev/warrant-js";
import { Client as WarrantClient } from "@warrantdev/warrant-js";

// A valid session token is required to initialize the Client
const warrant = new WarrantClient('client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=', sessionToken);
Expand All @@ -52,7 +52,7 @@ const warrant = new WarrantClient('client_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICb
// Example Scenario:
// An e-commerce website where Store Owners can edit their own Store's info
//
if (await warrant.isAuthorized("edit_stores")) {
if (await warrant.isAuthorized("store", storeId, "edit")) {
// Carry out logic to allow user to edit a Store
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@warrantdev/warrant-js",
"version": "0.0.2",
"version": "0.0.3",
"description": "JavaScript Library to Use Warrant in Client Applications",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 6 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export default class Client {
})
}

public async isAuthorized(permissionId: string): Promise<boolean> {
public async isAuthorized(objectType: string, objectId: string, relation: string): Promise<boolean> {
try {
await this.httpClient.get(`/authorize/${permissionId}`);
await this.httpClient.post("/sessions/authorize", {
objectType,
objectId,
relation,
});

return true;
} catch (e) {
Expand Down

0 comments on commit 0ca90e4

Please sign in to comment.