Skip to content

Commit

Permalink
feat: create an utility component to render content only for authenti…
Browse files Browse the repository at this point in the history
…cated users

Refs: SHELL-265
  • Loading branch information
gnekoz authored Dec 17, 2024
1 parent 7440c7b commit d3cba01
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api-extractor/carbonio-shell-ui.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { LinkProps } from 'react-router-dom';
import type { ModalProps } from '@zextras/carbonio-design-system';
import type { Properties } from 'posthog-js';
import { default as React_2 } from 'react';
import type { ReactNode } from 'react';
import type { TFunction } from 'i18next';
import type { To } from 'history';

Expand Down Expand Up @@ -326,6 +327,14 @@ export type AudioNotificationConfig = {
sound?: string;
};

// @public (undocumented)
export const AuthGuard: ({ children }: AuthGuardProps) => ReactNode;

// @public (undocumented)
export type AuthGuardProps = {
children: ReactNode;
};

// @public (undocumented)
export type BadgeInfo = {
show: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/boot/app/app-direct-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ export type { AccountMenuAction } from '../../utility-bar/bar';
export { useTracker } from '../../tracker/tracker';

export { AppContextProvider } from './app-context-provider';

export type { AuthGuardProps } from '../../ui-extras/auth-guard';
export { AuthGuard } from '../../ui-extras/auth-guard';
38 changes: 38 additions & 0 deletions src/ui-extras/auth-guard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import React from 'react';

import { AuthGuard } from './auth-guard';
import * as accountStoreHooks from '../store/account/hooks';
import { setup, screen } from '../tests/utils';

describe('AuthGuard', () => {
it('should render the child component when the user is authenticated', () => {
const useAuthenticated = jest.spyOn(accountStoreHooks, 'useAuthenticated');
useAuthenticated.mockReturnValue(true);

setup(
<AuthGuard>
<p>Authenticated</p>
</AuthGuard>
);

expect(screen.getByText('Authenticated')).toBeInTheDocument();
});

it('should not render the child component when the user is authenticated', () => {
const useAuthenticated = jest.spyOn(accountStoreHooks, 'useAuthenticated');
useAuthenticated.mockReturnValue(false);

setup(
<AuthGuard>
<p>Authenticated</p>
</AuthGuard>
);

expect(screen.queryByText('Authenticated')).not.toBeInTheDocument();
});
});
19 changes: 19 additions & 0 deletions src/ui-extras/auth-guard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { ReactNode } from 'react';
import React from 'react';

import { useAuthenticated } from '../store/account';

export type AuthGuardProps = {
children: ReactNode;
};

export const AuthGuard = ({ children }: AuthGuardProps): ReactNode => {
const isAuthenticated = useAuthenticated();

return isAuthenticated ? <>{children}</> : null;
};

0 comments on commit d3cba01

Please sign in to comment.