Skip to content

Commit

Permalink
Merge pull request #10 from warrant-dev/UpdateToWarrantJsV3
Browse files Browse the repository at this point in the history
Update to warrant-js v3
  • Loading branch information
stanleyphu authored Mar 6, 2024
2 parents 946ac01 + 406a25c commit 7eb9908
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
64 changes: 9 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
},
"homepage": "https://github.com/warrant-dev/vue-warrant#readme",
"dependencies": {
"@warrantdev/warrant-js": "^1.0.0"
},
"peerDependencies": {
"@warrantdev/warrant-js": "^3.4.0",
"vue": "^2.6.14"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/ProtectedView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ProtectedView = Vue.extend({
throw new Error("Invalid or no warrants provided to ProtectedView");
}

this.isAuthorized = await this.$warrant.hasWarrant({ op: this.op, warrants: this.warrants });
this.isAuthorized = await this.$warrant.checkMany({ op: this.op, warrants: this.warrants });
},
render(createElement: CreateElement): any {
if (this.isAuthorized) {
Expand Down
39 changes: 36 additions & 3 deletions src/Warrant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from "vue";
import { Client as WarrantClient, WarrantCheck } from "@warrantdev/warrant-js";
import { WarrantClient, Check, CheckMany, FeatureCheck, PermissionCheck } from "@warrantdev/warrant-js";

export interface PluginOptions {
clientKey: string;
Expand Down Expand Up @@ -32,13 +32,46 @@ const useWarrant = (options: PluginOptions): Vue => {

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

this.isLoading = true;
const isAuthorized = await new WarrantClient(options.clientKey, this.sessionToken).isAuthorized(warrantCheck);
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).check(warrantCheck);
this.isLoading = false;

return isAuthorized;
},
async checkMany(warrantCheck: CheckMany): Promise<boolean> {
if (!this.sessionToken) {
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.");
}

this.isLoading = true;
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).checkMany(warrantCheck);
this.isLoading = false;

return isAuthorized;
},
async hasPermission(warrantCheck: PermissionCheck): Promise<boolean> {
if (!this.sessionToken) {
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.");
}

this.isLoading = true;
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).hasPermission(warrantCheck);
this.isLoading = false;

return isAuthorized;
},
async hasFeature(warrantCheck: FeatureCheck): Promise<boolean> {
if (!this.sessionToken) {
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.");
}

this.isLoading = true;
const isAuthorized = await new WarrantClient({ clientKey: options.clientKey, sessionToken: this.sessionToken }).hasFeature(warrantCheck);
this.isLoading = false;

return isAuthorized;
Expand Down
33 changes: 23 additions & 10 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WarrantCheck } from "@warrantdev/warrant-js";
import { CheckMany, WarrantObjectLiteral, isWarrantObject } from "@warrantdev/warrant-js";

export interface MiddlewareOptions extends WarrantCheck {
export interface MiddlewareOptions extends CheckMany {
redirectTo: string;
}

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

let warrantsToCheck = [...warrants].map(warrant => ({...warrant}));
warrantsToCheck.forEach((warrant) => {
if (to.params[warrant.objectId]) {
if (isWarrantObject(warrant.object)) {
if (to.params[warrant.object?.getObjectId()]) {
warrant.object = {
objectType: warrant.object.getObjectType(),
objectId: to.params[warrant.object.getObjectId()]
}
}

/** @ts-ignore */
warrant.objectId = to.params[warrant.objectId];
}
if (!warrant.object?.getObjectId()) {
throw new Error("Invalid or no objectId provided for ProtectedRoute");
}
} else {
if (to.params[warrant.object?.objectId]) {
warrant.object.objectId = to.params[warrant.object.objectId];
}

if (!warrant.objectId) {
throw new Error("Invalid or no objectId provided for ProtectedRoute");
if (!warrant.object?.objectId) {
throw new Error("Invalid or no objectId provided for ProtectedRoute");
}
}
})
})

const hasWarrant = await vm.$warrant.hasWarrant({ op, warrants: warrantsToCheck });
if (!hasWarrant) {
const isAuthorized = await vm.$warrant.checkMany({ op, warrants: warrantsToCheck });
if (!isAuthorized) {
next({ path: redirectTo });
} else {
next();
Expand Down

0 comments on commit 7eb9908

Please sign in to comment.