Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
doregg committed Oct 22, 2023
1 parent fe5be15 commit b296b2d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
71 changes: 60 additions & 11 deletions packages/vue/src/auth/entitlements.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore
import { inject, computed } from 'vue';
import { EntitledToOptions, Entitlement } from '@frontegg/types';
import { EntitledToOptions, Entitlement, CustomAttributes, Attributes } from '@frontegg/types';

import {
getFeatureEntitlements,
Expand All @@ -9,36 +9,85 @@ import {
AuthState,
} from '@frontegg/redux-store';

import { FeatureFlags, USE_ENTITLEMENTS_V2_ENDPOINT_FF } from '@frontegg/rest-api';

import { authStateKey } from '../constants';
import { useFronteggStore } from '../auth/mapAuthState';

const getEntitlementsState = () => {
/**
* @returns user state
*/
const useGetUserState = () => {
const authState = inject(authStateKey) as AuthState;
return authState.user?.entitlements as any;
return authState.user;
};

/**
* @returns true when need to use entitlements V2 API
*/
const useIsV2API = () => {
// TODO: check should it be immediate or we can recursively inject (check when app name change?)
const { appName } = useFronteggStore();
const [useEntitlementsV2] = FeatureFlags.getFeatureFlags([USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
return useEntitlementsV2;
};

/**
* @param customAttributes consumer attributes
* @returns is entitled query data including: entitltments state, final attributes (consumer and frontegg) and API version to use
*/
const useEntitlementsQueryData = (customAttributes?: CustomAttributes) => {
const user = useGetUserState();
const entitlements = user?.entitlements;
const isV2 = useIsV2API();

const attributes = {
custom: customAttributes,
frontegg: user,
} as Attributes | undefined;

return {
entitlements,
attributes,
isV2
};
};

/**
@param key feature
@param key feature key
@param customAttributes consumer attributes
@returns if the user is entitled to the given feature. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
export const useFeatureEntitlements = (key: string): Entitlement => {
return computed(() => getFeatureEntitlements(getEntitlementsState(), key));
export const useFeatureEntitlements = (key: string, customAttributes?: CustomAttributes): Entitlement => {
return computed(() => {
const { entitlements, attributes, isV2 } = useEntitlementsQueryData(customAttributes);
return getFeatureEntitlements(entitlements, key, attributes, isV2)
});
};

/**
@param key permission
@param key permission key
@param customAttributes consumer attributes
@returns if the user is entitled to the given permission. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
export const usePermissionEntitlements = (key: string): Entitlement => {
return computed(() => getPermissionEntitlements(getEntitlementsState(), key));
export const usePermissionEntitlements = (key: string, customAttributes?: CustomAttributes): Entitlement => {
return computed(() => {
const { entitlements, attributes, isV2 } = useEntitlementsQueryData(customAttributes);
return getPermissionEntitlements(entitlements, key, attributes, isV2)
});
};

/**
@param entitledToOptions - including permission or feature key
@param customAttributes consumer attributes
@returns if the user is entitled to the given permission or feature. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
export const useEntitlements = (entitledToOptions: EntitledToOptions): Entitlement => {
return computed(() => getEntitlements(getEntitlementsState(), entitledToOptions));
export const useEntitlements = (entitledToOptions: EntitledToOptions, customAttributes?: CustomAttributes): Entitlement => {
return computed(() => {
const { entitlements, attributes, isV2 } = useEntitlementsQueryData(customAttributes);
return getEntitlements(entitlements as any, entitledToOptions, attributes, isV2)
});
};
10 changes: 5 additions & 5 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AdminPortal, initialize } from '@frontegg/js';
import { FronteggAuthService } from './auth/service';
import { connectMapState, connectFronteggStoreV3 } from './auth/mapAuthState';
import { ContextHolder, FronteggFrameworks } from '@frontegg/rest-api';
import { EntitledToOptions } from '@frontegg/types';
import { EntitledToOptions, CustomAttributes } from '@frontegg/types';
import {
authStateKey,
fronteggAuthKey,
Expand Down Expand Up @@ -243,10 +243,10 @@ const Frontegg: PluginObject<PluginOptions> | any = {
this.fronteggAuth = Vue.fronteggAuth;
this.loginWithRedirect = loginWithRedirect.bind(this);

// _entitlements was added for to make the computed property reactive, then it will get updated
this.getFeatureEntitlements = (_entitlements: User['entitlements'], key: string) => fronteggApp.getFeatureEntitlements(key);
this.getPermissionEntitlements = (_entitlements: User['entitlements'], key: string) => fronteggApp.getPermissionEntitlements(key);
this.getEntitlements = (_entitlements: User['entitlements'], entitledToOptions: EntitledToOptions) => fronteggApp.getEntitlements(entitledToOptions);
// _user was added for to make the computed property reactive, then it will get updated
this.getFeatureEntitlements = (_user: User | undefined | null, key: string, customAttributes?: CustomAttributes) => fronteggApp.getFeatureEntitlements(key, customAttributes);
this.getPermissionEntitlements = (_user: User | undefined | null, key: string, customAttributes?: CustomAttributes) => fronteggApp.getPermissionEntitlements(key, customAttributes);
this.getEntitlements = (_user: User | undefined | null, entitledToOptions: EntitledToOptions, customAttributes?: CustomAttributes) => fronteggApp.getEntitlements(entitledToOptions, customAttributes);
this.loadEntitlements = loadEntitlements;

connectMapState(this);
Expand Down
15 changes: 9 additions & 6 deletions packages/vue/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EnhancedStore } from '@reduxjs/toolkit';
import { Unsubscribe } from 'redux';
import { Entitlement, User } from '@frontegg/redux-store';
import { EntitledToOptions, LoadEntitlementsCallback } from '@frontegg/types';
import { EntitledToOptions, LoadEntitlementsCallback, CustomAttributes } from '@frontegg/types';
import { FronteggPluginService } from './interfaces';

declare module 'vue/types/vue' {
Expand All @@ -20,25 +20,28 @@ declare module 'vue/types/vue' {
loginWithRedirect: () => void;

/**
@param key feature
@param key feature key
@param customAttributes consumer attributes
@returns if the user is entitled to the given feature. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
getFeatureEntitlements: (_entitlements: User['entitlements'], key: string) => Entitlement;
getFeatureEntitlements: (_user: User | undefined | null, key: string, customAttributes?: CustomAttributes) => Entitlement;

/**
@param key permission
@param key permission key
@param customAttributes consumer attributes
@returns if the user is entitled to the given permission. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
getPermissionEntitlements: (_entitlements: User['entitlements'], key: string) => Entitlement;
getPermissionEntitlements: (_user: User | undefined | null, key: string, customAttributes?: CustomAttributes) => Entitlement;

/**
@param entitledToOptions - including permission or feature key
@param customAttributes consumer attributes
@returns if the user is entitled to the given permission or feature. Attaching the justification if not
@throws when entitlement is not enabled via frontegg options
*/
getEntitlements: (_entitlements: User['entitlements'], entitledToOptions: EntitledToOptions) => Entitlement;
getEntitlements: (_user: User | undefined | null, entitledToOptions: EntitledToOptions, customAttributes?: CustomAttributes) => Entitlement;

/**
* Load entitlements
Expand Down

0 comments on commit b296b2d

Please sign in to comment.