Skip to content

Commit

Permalink
Merge pull request #389 from frontegg/add-redirect-uri-to-frontegg-au…
Browse files Browse the repository at this point in the history
…th-guard

FR-14757 - Add redirect uri to frontegg auth guard
  • Loading branch information
yuvalotem1 authored Jan 3, 2024
2 parents 583eb21 + c771d02 commit 10f772a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/vue/src/auth/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ declare module 'vue/types/vue' {
mapTenantsState: () => { tenantsState: TenantsState };
}
}

export type FronteggAuthGuardOptions = {
redirectUrl?: string;
};
15 changes: 12 additions & 3 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 } from '@frontegg/redux-store';
import { AuthState, EnhancedStore, FRONTEGG_AFTER_AUTH_REDIRECT_URL } from '@frontegg/redux-store';
import { FronteggAuthService } from './service';
import VueRouter from 'vue-router';
import {
Expand All @@ -32,9 +32,10 @@ import {
unsubscribeFronteggStoreKey,
loadEntitlementsKey,
} from '../constants';
import { FronteggAuthGuardOptions } from './interfaces';

const mapSubState = (statePrefix: string, propertyName?: string) =>
function() {
function () {
const obj = { [propertyName ?? statePrefix.substring('auth.'.length)]: (state: any) => state };
// @ts-ignore
return objectMappers(statePrefix, obj, defaultGetterGenerator).bind(this)();
Expand Down Expand Up @@ -190,7 +191,12 @@ export const useFrontegg = () => {
};
};

export const useFronteggAuthGuard = () => {
/**
* Redirect to login page if the user is not authenticated
* @param redirectUrl the url to redirect after hosted login
*/
export const useFronteggAuthGuard = (options?: FronteggAuthGuardOptions) => {
const { redirectUrl } = options ?? {}
const fronteggAuth = useFronteggAuth();
const fronteggOptions = inject(fronteggOptionsKey) as any;
const authState = useAuthState();
Expand All @@ -208,6 +214,9 @@ export const useFronteggAuthGuard = () => {
if (!isAuthRoutes(fronteggAuth.router?.currentRoute.path!) && !authState.isAuthenticated && !authState.isLoading) {
if (fronteggOptions.hostedLoginBox) {
fronteggStore.dispatch({ type: 'auth/setState', payload: { isLoading: true } });
if (redirectUrl) {
localStorage.setItem(FRONTEGG_AFTER_AUTH_REDIRECT_URL, redirectUrl);
}
fronteggAuth.loginActions.requestHostedLoginAuthorize();
} else {
router.push(authState.routes.loginUrl);
Expand Down

0 comments on commit 10f772a

Please sign in to comment.