Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR-14999 - Fixed support for vue-router 4 in vue 3 #398

Merged
merged 7 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading