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 2 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
17 changes: 11 additions & 6 deletions packages/vue/src/auth/mapAuthState.ts
Original file line number Diff line number Diff line change
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()
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
if (!path.startsWith(authState.routes.hostedLoginRedirectUrl ?? "/oauth/callback")) {
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
fronteggStore.dispatch({ type: 'auth/setState', payload: { isLoading: true } });
fronteggAuth.loginActions.requestHostedLoginAuthorize();
}
Expand Down Expand Up @@ -245,14 +245,19 @@ export const useFronteggAuthGuard = (options?: FronteggAuthGuardOptions) => {
const fronteggStore = useFronteggStore() as EnhancedStore;

const isAuthRoutes = (path: string) => {
const pathname = new URL(path, window.location.origin).pathname
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
const { routes } = authState;
return Object.values(routes)
.filter(path => path !== routes.authenticatedUrl)
.includes(path);
.filter(route => route !== routes.authenticatedUrl)
.includes(pathname);
};

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

// @ts-ignore
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
const route = fronteggAuth.getCurrentRoute();

if (!isAuthRoutes(route) && !authState.isAuthenticated && !authState.isLoading) {
if (fronteggOptions.hostedLoginBox) {
fronteggStore.dispatch({ type: 'auth/setState', payload: { isLoading: true } });
if (redirectUrl) {
Expand Down
18 changes: 17 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,20 @@ export class FronteggAuthService implements FronteggPluginService {
}
}
};


getCurrentRoute = () => {
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?.path) {
// @ts-ignore vue-router v4
route = this.router?.currentRoute?.value?.fullPath;
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
}

return route
};
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 3 additions & 2 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ const Frontegg: PluginObject<PluginOptions> | any = {
}, 10);

const isAuthRoutes = (path: string) => {
const pathname = new URL(path, window.location.origin).pathname
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
const { routes } = Vue.fronteggAuth;
return Object.values(routes)
.filter(path => path != routes.authenticatedUrl)
.includes(path);
.filter(route => route != routes.authenticatedUrl)
.includes(pathname);
frontegg-david marked this conversation as resolved.
Show resolved Hide resolved
};

const authorizedContentGuard = (_this: any) => {
Expand Down
Loading