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

Feature : Added server side verification for all routes . #3491

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Admin Docs](/)

***

# Variable: VERIFY\_ROLE

> `const` **VERIFY\_ROLE**: `DocumentNode`

Defined in: [src/GraphQl/Queries/Queries.ts:867](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/GraphQl/Queries/Queries.ts#L867)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **default**(): `Element`

Defined in: [src/components/SecuredRoute/SecuredRoute.tsx:16](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/SecuredRoute/SecuredRoute.tsx#L16)
Defined in: [src/components/SecuredRoute/SecuredRoute.tsx:18](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/SecuredRoute/SecuredRoute.tsx#L18)

A route guard that checks if the user is logged in and has the necessary permissions.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **default**(): `Element`

Defined in: [src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx:14](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx#L14)
Defined in: [src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx:16](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx#L16)

A component that guards routes by checking if the user is logged in.
If the user is logged in and does not have 'AdminFor' set, the child routes are rendered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **getLanguageName**(`code`): `string`

Defined in: [src/screens/MemberDetail/MemberDetail.tsx:742](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L742)
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:744](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L744)

## Parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **prettyDate**(`param`): `string`

Defined in: [src/screens/MemberDetail/MemberDetail.tsx:732](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L732)
Defined in: [src/screens/MemberDetail/MemberDetail.tsx:734](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/MemberDetail/MemberDetail.tsx#L734)

## Parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **default**(): `Element`

Defined in: [src/screens/PageNotFound/PageNotFound.tsx:15](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/PageNotFound/PageNotFound.tsx#L15)
Defined in: [src/screens/PageNotFound/PageNotFound.tsx:16](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/PageNotFound/PageNotFound.tsx#L16)

The `PageNotFound` component displays a 404 error page when a user navigates to a non-existent route.
It shows a message indicating that the page was not found and provides a link to redirect users back
Expand Down
4 changes: 4 additions & 0 deletions src/App.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const MOCKS = [
mobile: '+8912313112',
},
},
verifyRole: {
isAuthorized: false, // Ensure this field exists
role: 'user', // Adjust this based on the expected role
},
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down
8 changes: 8 additions & 0 deletions src/GraphQl/Queries/Queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,14 @@ export const GET_COMMUNITY_SESSION_TIMEOUT_DATA = gql`
}
}
`;
export const VERIFY_ROLE = gql`
query verifyRole {
verifyRole {
isAuthorized
role
}
}
`;

// get the list of Action Item Categories
export { ACTION_ITEM_CATEGORY_LIST } from './ActionItemCategoryQueries';
Expand Down
59 changes: 49 additions & 10 deletions src/components/SecuredRoute/SecuredRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { useQuery } from '@apollo/client';
import { VERIFY_ROLE } from 'GraphQl/Queries/Queries';
import React, { useEffect } from 'react';
import { Navigate, Outlet, useLocation } from 'react-router-dom';
import { toast } from 'react-toastify';
import PageNotFound from 'screens/PageNotFound/PageNotFound';
import useLocalStorage from 'utils/useLocalstorage';
Expand All @@ -14,14 +16,51 @@
* @returns The JSX element representing the secured route.
*/
const SecuredRoute = (): JSX.Element => {
const isLoggedIn = getItem('IsLoggedIn');
const adminFor = getItem('AdminFor');

return isLoggedIn === 'TRUE' ? (
<>{adminFor != null ? <Outlet /> : <PageNotFound />}</>
) : (
<Navigate to="/" replace />
);
const location = useLocation();
const { data, loading, error, refetch } = useQuery(VERIFY_ROLE, {
skip: !getItem('token'),
context: {
headers: {
Authorization: `Bearer ${getItem('token')}`,
},
},
});
const [token, setToken] = React.useState(getItem('token'));

useEffect(() => {
const newToken = getItem('token');
if (newToken !== token) {
setToken(newToken);
}
}, []);

useEffect(() => {
refetch(); // Refetch when token updates
}, [token]);

if (loading) {
return <div> Loading.....</div>;
} else if (error) {
return <div>Error During Routing ...</div>;

Check failure on line 44 in src/components/SecuredRoute/SecuredRoute.tsx

View workflow job for this annotation

GitHub Actions / Test Application

src/App.spec.tsx > Testing the App Component > Component should be rendered properly and user is logged out

TypeError: Cannot read properties of undefined (reading 'verifyRole') ❯ SecuredRoute src/components/SecuredRoute/SecuredRoute.tsx:44:12 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22
} else {
const isLoggedIn = data.verifyRole.isAuthorized;
const role = data.verifyRole.role;
const restrictedRoutesForAdmin = ['/member', '/users', '/communityProfile'];
if (isLoggedIn) {
if (role == 'superAdmin') {
return <Outlet />;
} else if (role == 'admin') {
if (restrictedRoutesForAdmin.includes(location.pathname)) {
return <PageNotFound />;
}
return <Outlet />;
} else {
return <PageNotFound />;
}
} else {
return <Navigate to="/" replace />;
}
}
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
};

// Time constants for session timeout and inactivity interval
Expand Down
Loading
Loading