Skip to content

Commit

Permalink
Merge pull request #398 from frontegg/FR-14999-fix-router
Browse files Browse the repository at this point in the history
FR-14999 - Add fallback support for currentRoute.value.fullPath
  • Loading branch information
doregg authored Jan 28, 2024
2 parents ca98ffa + dbd5308 commit d377b76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
19 changes: 7 additions & 12 deletions packages/vue/src/auth/mapAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
AuthActions,
} from '@frontegg/redux-store';
import { ActionsHolder } from './ActionsHolder';
import { AuthState, EnhancedStore, FRONTEGG_AFTER_AUTH_REDIRECT_URL, isSteppedUp, IsSteppedUpOptions } from '@frontegg/redux-store';
import { AuthState, EnhancedStore, FRONTEGG_AFTER_AUTH_REDIRECT_URL, isSteppedUp, IsSteppedUpOptions, defaultFronteggRoutes } from '@frontegg/redux-store';
import { FronteggAuthService } from './service';
import VueRouter from 'vue-router';
import {
Expand Down Expand Up @@ -173,7 +173,7 @@ export const useLoadEntitlements = () => {
};

/**
* @returns user state
* @returns user state
*/
const useGetUserState = () => {
const authState = inject(authStateKey) as AuthState;
Expand Down Expand Up @@ -215,8 +215,8 @@ export const useFrontegg = () => {
const fronteggStore = useFronteggStore() as EnhancedStore;

const loginWithRedirect = () => {
// @ts-ignore
if (!fronteggAuth.router?.currentRoute.path.startsWith(authState.routes.hostedLoginRedirectUrl)) {
const path = fronteggAuth.getCurrentRoute();
if (!path.startsWith(authState.routes.hostedLoginRedirectUrl ?? defaultFronteggRoutes.hostedLoginRedirectUrl)) {
fronteggStore.dispatch({ type: 'auth/setState', payload: { isLoading: true } });
fronteggAuth.loginActions.requestHostedLoginAuthorize();
}
Expand Down Expand Up @@ -244,15 +244,10 @@ export const useFronteggAuthGuard = (options?: FronteggAuthGuardOptions) => {
const router = inject(routerKey) as VueRouter;
const fronteggStore = useFronteggStore() as EnhancedStore;

const isAuthRoutes = (path: string) => {
const { routes } = authState;
return Object.values(routes)
.filter(path => path !== routes.authenticatedUrl)
.includes(path);
};

const checkGuard = () => {
if (!isAuthRoutes(fronteggAuth.router?.currentRoute.path!) && !authState.isAuthenticated && !authState.isLoading) {
const route = fronteggAuth.getCurrentRoute();

if (!fronteggAuth.isAuthRoutes(route) && !authState.isAuthenticated && !authState.isLoading) {
if (fronteggOptions.hostedLoginBox) {
fronteggStore.dispatch({ type: 'auth/setState', payload: { isLoading: true } });
if (redirectUrl) {
Expand Down
25 changes: 24 additions & 1 deletion packages/vue/src/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class FronteggAuthService implements FronteggPluginService {
apiTokensActions,
securityPolicyActions,
tenantsActions,
}).forEach(([key, actions]: any) => {
}).forEach(([ key, actions ]: any) => {
Object.assign(this, { [key]: bindActionCreators(actions, this.store!.dispatch) });
});

Expand Down Expand Up @@ -152,4 +152,27 @@ export class FronteggAuthService implements FronteggPluginService {
}
}
};


getCurrentRoute = (): string => {
let route: string = '/';
if (this.router?.currentRoute?.path) {
// vue-router v3
route = this.router?.currentRoute?.path;
}
// @ts-ignore vue-router v4
if (this.router?.currentRoute?.value?.fullPath) {
// @ts-ignore vue-router v4
route = this.router?.currentRoute?.value?.fullPath;
}

return route;
};

isAuthRoutes = (path: string): boolean => {
const pathname = new URL(path, window.location.origin).pathname;
return Object.values(this.routes)
.filter(route => route != this.routes.authenticatedUrl)
.includes(pathname);
};
}
8 changes: 1 addition & 7 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,9 @@ const Frontegg: PluginObject<PluginOptions> | any = {
}
}, 10);

const isAuthRoutes = (path: string) => {
const { routes } = Vue.fronteggAuth;
return Object.values(routes)
.filter(path => path != routes.authenticatedUrl)
.includes(path);
};

const authorizedContentGuard = (_this: any) => {
if (!_this.authorizedContent || isAuthRoutes(_this.$route.path)) {
if (!_this.authorizedContent || Vue.fronteggAuth.isAuthRoutes(_this.$route.path)) {
return;
}
if (!Vue.fronteggAuth.loading && !Vue.fronteggAuth.isAuthenticated) {
Expand Down

0 comments on commit d377b76

Please sign in to comment.