Skip to content

Commit

Permalink
refactor: change the code to remove duplication in routes. (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukanya-rath authored Jan 23, 2024
1 parent b08eff3 commit fde305a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 27 deletions.
10 changes: 9 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express, { Request, Response } from 'express';
import express, { NextFunction, Request, Response } from 'express';

import bodyParser from 'body-parser';
import cors from 'cors';
Expand Down Expand Up @@ -236,6 +236,14 @@ apiRouter.get('/', (_req, res) => {
res.sendStatus(200); // generally for route verification and health check.
});
apiRouter.use('/auth', authRouter);

// check for valid passport session and backend token for all routes below.
apiRouter.use(
passport.authenticate('jwt', { session: false }),
(req: Request, res: Response, next: NextFunction) => {
auth.isValidBackendToken()(req, res, next);
},
);
apiRouter.use('/user', userRouter);
apiRouter.use('/v1/file-upload', fileUploadRouter);
apiRouter.use('/v1/codes', codeRouter);
Expand Down
7 changes: 0 additions & 7 deletions backend/src/v1/routes/code-routes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import express, { Request, Response } from 'express';
import passport from 'passport';
import { auth } from '../services/auth-service';
import { codeService } from '../services/code-service';
import { utils } from '../services/utils-service';

const isValidBackendToken = auth.isValidBackendToken();
const router = express.Router();

router.get(
'/employee-count-ranges',
passport.authenticate('jwt', { session: false }),
utils.asyncHandler(isValidBackendToken),
utils.asyncHandler(async (_req: Request, res: Response) => {
const body = await codeService.getAllEmployeeCountRanges();
res.status(200).json(body);
Expand All @@ -19,8 +14,6 @@ router.get(

router.get(
'/naics-codes',
passport.authenticate('jwt', { session: false }),
utils.asyncHandler(isValidBackendToken),
utils.asyncHandler(async (_req: Request, res: Response) => {
const body = await codeService.getAllNaicsCodes();
res.status(200).json(body);
Expand Down
6 changes: 0 additions & 6 deletions backend/src/v1/routes/file-upload-routes.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import express, { Request, Response } from 'express';
import passport from 'passport';
import { auth } from '../services/auth-service';
import { fileUploadService } from '../services/file-upload-service';
import { utils } from '../services/utils-service';

const fileUploadRouter = express.Router();
fileUploadRouter.post(
'/',
passport.authenticate('jwt', { session: false }, undefined),
utils.asyncHandler(auth.isValidBackendToken()),
utils.asyncHandler(fileUploadService.handleFileUpload),
);

fileUploadRouter.get(
'/',
passport.authenticate('jwt', { session: false }, undefined),
utils.asyncHandler(auth.isValidBackendToken()),
utils.asyncHandler(async (_req: Request, res: Response) => {
const companies = await fileUploadService.getCompanies();
res.status(200).json(companies);
Expand Down
6 changes: 1 addition & 5 deletions backend/src/v1/routes/report-routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import express, { Request, Response } from 'express';
import passport from 'passport';
import { auth } from '../services/auth-service';
import { reportService, enumReportStatus } from '../services/report-service';
import HttpStatus from 'http-status-codes';
import { utils } from '../services/utils-service';
Expand All @@ -13,14 +11,12 @@ const reportRouter = express.Router();
*/
reportRouter.get(
'/',
passport.authenticate('jwt', { session: false }, undefined),
utils.asyncHandler(auth.isValidBackendToken()),
utils.asyncHandler(
async (
req: Request<null, null, null, { status: enumReportStatus }>,
res: Response,
) => {
// verifiy business guid
// verify business guid
const businessGuid =
utils.getSessionUser(req)?._json?.bceid_business_guid;
if (!businessGuid)
Expand Down
9 changes: 1 addition & 8 deletions backend/src/v1/routes/user-info-routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import passport from 'passport';
import express from 'express';
import { auth } from '../services/auth-service';
import { utils } from '../services/utils-service';
const isValidBackendToken = auth.isValidBackendToken();
const router = express.Router();
router.get(
'/',
passport.authenticate('jwt', { session: false }),
utils.asyncHandler(isValidBackendToken),
utils.asyncHandler(auth.getUserInfo),
);
router.get('/', utils.asyncHandler(auth.getUserInfo));
export = router;

0 comments on commit fde305a

Please sign in to comment.