diff --git a/backend-mock/src/app.ts b/backend-mock/src/app.ts index 853d34f..26369ca 100644 --- a/backend-mock/src/app.ts +++ b/backend-mock/src/app.ts @@ -10,11 +10,11 @@ mockApi.use(express.json()) mockApi.use(requestLogger) // Enable CORS based on the corsOptions configuration mockApi.use('*', (_, res, next) => { - corsHeaders.forEach((option) => { - const key = Object.keys(option)[0] - res.setHeader(key, option[key]) - }) - next() + corsHeaders.forEach((option) => { + const key = Object.keys(option)[0] + res.setHeader(key, option[key]) + }) + next() }) // Routes diff --git a/backend-mock/src/index.ts b/backend-mock/src/index.ts index 5f2c2a4..af0bd72 100644 --- a/backend-mock/src/index.ts +++ b/backend-mock/src/index.ts @@ -5,13 +5,13 @@ import mockApi from './app' // Load data AppDataSource.initialize() - .then(async () => { - console.log('Loading users from the database...') - const users = await AppDataSource.manager.find(User) - }) - .catch((error) => console.log(error)) + .then(async () => { + console.log('Loading users from the database...') + const users = await AppDataSource.manager.find(User) + }) + .catch((error) => console.log(error)) // Start the server mockApi.listen(PORT, () => { - console.log(`Server is running on ${PORT}`) + console.log(`Server is running on ${PORT}`) }) diff --git a/backend-mock/src/utils/config.ts b/backend-mock/src/utils/config.ts index dccdf9f..cbdb72e 100644 --- a/backend-mock/src/utils/config.ts +++ b/backend-mock/src/utils/config.ts @@ -6,7 +6,7 @@ export const JWT_SECRET = process.env.MOCK_JWT_SECRET export const JWT_EXPIRES_IN = process.env.MOCK_JWT_EXPIRES_IN export const JWT_REFRESH_EXPIRES_IN = process.env.MOCK_JWT_REFRESH_EXPIRES_IN export const corsHeaders = [ - { 'Access-Control-Allow-Origin': process.env.CLIENT_URL }, - { 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS' }, - { 'Access-Control-Allow-Headers': 'Content-Type, Authorization' }, + { 'Access-Control-Allow-Origin': process.env.CLIENT_URL }, + { 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS' }, + { 'Access-Control-Allow-Headers': 'Content-Type, Authorization' }, ] diff --git a/backend-mock/src/utils/middleware.ts b/backend-mock/src/utils/middleware.ts index 5709f3d..252a896 100644 --- a/backend-mock/src/utils/middleware.ts +++ b/backend-mock/src/utils/middleware.ts @@ -3,51 +3,51 @@ import * as express from 'express' import logger from './logger' export const authenticator = ( - req: express.Request, - _res: express.Response, - next: express.NextFunction, + req: express.Request, + _res: express.Response, + next: express.NextFunction, ) => { - // Check if bearer token exists - const authorization = req.get('authorization') - if (!authorization || !authorization.toLowerCase().startsWith('Bearer ')) { - throw new MockApiError(401, 'not authenticated') - } + // Check if bearer token exists + const authorization = req.get('authorization') + if (!authorization || !authorization.toLowerCase().startsWith('bearer ')) { + throw new MockApiError(401, 'not authenticated') + } - // Verify token - // const token = authorization.substring(7) - // jwt.verify(token, JWT_SECRET) + // Verify token + // const token = authorization.substring(7) + // jwt.verify(token, JWT_SECRET) - // Add id of caller to the req object - // req.userId = userId; + // Add id of caller to the req object + // req.userId = userId; - next() + next() } export const errorHandler = ( - error: unknown, - _req: express.Request, - res: express.Response, - next: express.NextFunction, + error: unknown, + _req: express.Request, + res: express.Response, + next: express.NextFunction, ) => { - // Json web token error - if (error instanceof Error && error.name === 'JsonWebTokenError') - return res.status(401).json({ error: 'invalid token' }) + // Json web token error + if (error instanceof Error && error.name === 'JsonWebTokenError') + return res.status(401).json({ error: 'invalid token' }) - // Internal app error - if (error instanceof MockApiError) - return res.status(error.status).json({ error: error.message }) + // Internal app error + if (error instanceof MockApiError) + return res.status(error.status).json({ error: error.message }) - return next(error) + return next(error) } export const notFoundRoute = (_req: express.Request, _res: express.Response) => { - throw new MockApiError(404, 'Not found') + throw new MockApiError(404, 'Not found') } export const requestLogger = (req, _res, next) => { - logger.info('Method:', req.method) - logger.info('Path: ', req.path) - logger.info('Body: ', req.body) - logger.info('---') - next() + logger.info('Method:', req.method) + logger.info('Path: ', req.path) + logger.info('Body: ', req.body) + logger.info('---') + next() }