Skip to content

Commit 7eb9908

Browse files
authored
Merge pull request #10 from warrant-dev/UpdateToWarrantJsV3
Update to warrant-js v3
2 parents 946ac01 + 406a25c commit 7eb9908

File tree

5 files changed

+70
-70
lines changed

5 files changed

+70
-70
lines changed

package-lock.json

Lines changed: 9 additions & 55 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
@@ -33,9 +33,9 @@
3333
},
3434
"homepage": "https://github.com/warrant-dev/vue-warrant#readme",
3535
"dependencies": {
36-
"@warrantdev/warrant-js": "^1.0.0"
3736
},
3837
"peerDependencies": {
38+
"@warrantdev/warrant-js": "^3.4.0",
3939
"vue": "^2.6.14"
4040
},
4141
"devDependencies": {

src/ProtectedView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ProtectedView = Vue.extend({
2222
throw new Error("Invalid or no warrants provided to ProtectedView");
2323
}
2424

25-
this.isAuthorized = await this.$warrant.hasWarrant({ op: this.op, warrants: this.warrants });
25+
this.isAuthorized = await this.$warrant.checkMany({ op: this.op, warrants: this.warrants });
2626
},
2727
render(createElement: CreateElement): any {
2828
if (this.isAuthorized) {

src/Warrant.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from "vue";
2-
import { Client as WarrantClient, WarrantCheck } from "@warrantdev/warrant-js";
2+
import { WarrantClient, Check, CheckMany, FeatureCheck, PermissionCheck } from "@warrantdev/warrant-js";
33

44
export interface PluginOptions {
55
clientKey: string;
@@ -32,13 +32,46 @@ const useWarrant = (options: PluginOptions): Vue => {
3232

3333
localStorage.setItem(LOCAL_STORAGE_KEY_SESSION_TOKEN, newSessionToken);
3434
},
35-
async hasWarrant(warrantCheck: WarrantCheck): Promise<boolean> {
35+
async check(warrantCheck: Check): Promise<boolean> {
3636
if (!this.sessionToken) {
3737
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.");
3838
}
3939

4040
this.isLoading = true;
41-
const isAuthorized = await new WarrantClient(options.clientKey, this.sessionToken).isAuthorized(warrantCheck);
41+
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).check(warrantCheck);
42+
this.isLoading = false;
43+
44+
return isAuthorized;
45+
},
46+
async checkMany(warrantCheck: CheckMany): Promise<boolean> {
47+
if (!this.sessionToken) {
48+
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.");
49+
}
50+
51+
this.isLoading = true;
52+
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).checkMany(warrantCheck);
53+
this.isLoading = false;
54+
55+
return isAuthorized;
56+
},
57+
async hasPermission(warrantCheck: PermissionCheck): Promise<boolean> {
58+
if (!this.sessionToken) {
59+
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.");
60+
}
61+
62+
this.isLoading = true;
63+
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).hasPermission(warrantCheck);
64+
this.isLoading = false;
65+
66+
return isAuthorized;
67+
},
68+
async hasFeature(warrantCheck: FeatureCheck): Promise<boolean> {
69+
if (!this.sessionToken) {
70+
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.");
71+
}
72+
73+
this.isLoading = true;
74+
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).hasFeature(warrantCheck);
4275
this.isLoading = false;
4376

4477
return isAuthorized;

src/middleware.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { WarrantCheck } from "@warrantdev/warrant-js";
1+
import { CheckMany, WarrantObjectLiteral, isWarrantObject } from "@warrantdev/warrant-js";
22

3-
export interface MiddlewareOptions extends WarrantCheck {
3+
export interface MiddlewareOptions extends CheckMany {
44
redirectTo: string;
55
}
66

@@ -11,18 +11,31 @@ export const authorize = (options: MiddlewareOptions) => {
1111

1212
let warrantsToCheck = [...warrants].map(warrant => ({...warrant}));
1313
warrantsToCheck.forEach((warrant) => {
14-
if (to.params[warrant.objectId]) {
14+
if (isWarrantObject(warrant.object)) {
15+
if (to.params[warrant.object?.getObjectId()]) {
16+
warrant.object = {
17+
objectType: warrant.object.getObjectType(),
18+
objectId: to.params[warrant.object.getObjectId()]
19+
}
20+
}
21+
1522
/** @ts-ignore */
16-
warrant.objectId = to.params[warrant.objectId];
17-
}
23+
if (!warrant.object?.getObjectId()) {
24+
throw new Error("Invalid or no objectId provided for ProtectedRoute");
25+
}
26+
} else {
27+
if (to.params[warrant.object?.objectId]) {
28+
warrant.object.objectId = to.params[warrant.object.objectId];
29+
}
1830

19-
if (!warrant.objectId) {
20-
throw new Error("Invalid or no objectId provided for ProtectedRoute");
31+
if (!warrant.object?.objectId) {
32+
throw new Error("Invalid or no objectId provided for ProtectedRoute");
33+
}
2134
}
22-
})
35+
})
2336

24-
const hasWarrant = await vm.$warrant.hasWarrant({ op, warrants: warrantsToCheck });
25-
if (!hasWarrant) {
37+
const isAuthorized = await vm.$warrant.checkMany({ op, warrants: warrantsToCheck });
38+
if (!isAuthorized) {
2639
next({ path: redirectTo });
2740
} else {
2841
next();

0 commit comments

Comments
 (0)