Skip to content

Commit

Permalink
Merge branch 'main' into feat/259_argument_reports_suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Monstarrrr committed Aug 14, 2024
2 parents fc40516 + 260d27d commit cc40bff
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
10 changes: 5 additions & 5 deletions backend-mock/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions backend-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})
6 changes: 3 additions & 3 deletions backend-mock/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
]
62 changes: 31 additions & 31 deletions backend-mock/src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit cc40bff

Please sign in to comment.