Skip to content

Commit

Permalink
OV-8: + util function for checking route in white routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanchousina committed Aug 21, 2024
1 parent 5a24c65 commit b4fbbde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions backend/src/common/plugins/auth/auth-jwt.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { tokenService } from '~/common/services/services.js';

import { ErrorMessage, Hook } from './enums/enums.js';
import { type Route } from './types/types.js';
import { isRouteInWhiteList } from './utils/utils.js';

type Options = {
routesWhiteList: Route[];
Expand All @@ -15,12 +16,7 @@ const authenticateJWT = fp<Options>((fastify, { routesWhiteList }, done) => {
fastify.decorateRequest('user', null);

fastify.addHook(Hook.PRE_HANDLER, async (request) => {
const isRouteInWhiteList = routesWhiteList.some(
(route) =>
route.path === request.url && route.method === request.method,
);

if (isRouteInWhiteList) {
if (isRouteInWhiteList(routesWhiteList, request)) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions backend/src/common/plugins/auth/utils/check-white-routes.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type FastifyRequest } from 'fastify';

import { type Route } from '../types/types.js';

const isRouteInWhiteList = (
routesWhiteList: Route[],
request: FastifyRequest,
): boolean => {
return routesWhiteList.some(
(route) =>
route.path === request.url && route.method === request.method,
);
};

export { isRouteInWhiteList };
1 change: 1 addition & 0 deletions backend/src/common/plugins/auth/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { isRouteInWhiteList } from './check-white-routes.util.js';

0 comments on commit b4fbbde

Please sign in to comment.