Skip to content

Commit

Permalink
fix(frontend): also allow REQUEST_VIEW perm through route guard
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCatLady committed Mar 29, 2021
1 parent 64494c1 commit 7153f4e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/hooks/useRouteGuard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Permission, useUser } from './useUser';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import { Permission, PermissionCheckOptions, useUser } from './useUser';

const useRouteGuard = (permission: Permission | Permission[]): void => {
const useRouteGuard = (
permission: Permission | Permission[],
options?: PermissionCheckOptions
): void => {
const router = useRouter();
const { user, hasPermission } = useUser();

useEffect(() => {
if (user && !hasPermission(permission)) {
if (user && !hasPermission(permission, options)) {
router.push('/');
}
}, [user, permission, router, hasPermission]);
}, [user, permission, router, hasPermission, options]);
};

export default useRouteGuard;
5 changes: 3 additions & 2 deletions src/hooks/useUser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import useSwr from 'swr';
import { MutatorCallback } from 'swr/dist/types';
import { UserType } from '../../server/constants/user';
import {
hasPermission,
Permission,
PermissionCheckOptions,
} from '../../server/lib/permissions';
import { UserType } from '../../server/constants/user';
import { MutatorCallback } from 'swr/dist/types';

export { Permission, UserType };
export type { PermissionCheckOptions };

export interface User {
id: number;
Expand Down
6 changes: 4 additions & 2 deletions src/pages/users/[userId]/requests.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import { NextPage } from 'next';
import React from 'react';
import RequestList from '../../../components/RequestList';
import useRouteGuard from '../../../hooks/useRouteGuard';
import { Permission } from '../../../hooks/useUser';

const UserRequestsPage: NextPage = () => {
useRouteGuard(Permission.MANAGE_REQUESTS);
useRouteGuard([Permission.MANAGE_REQUESTS, Permission.REQUEST_VIEW], {
type: 'or',
});
return <RequestList />;
};

Expand Down

0 comments on commit 7153f4e

Please sign in to comment.