Skip to content

Commit

Permalink
implement middlewares in api b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 9, 2024
1 parent 954de64 commit 69213cf
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 5 deletions.
2 changes: 2 additions & 0 deletions staff/marti-herms/project/V-HUB/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
9 changes: 9 additions & 0 deletions staff/marti-herms/project/V-HUB/api/handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import authenticateUserHandler from './authenticateUserHandler.js'
import registerUserHandler from './registerUserHandler.js'

const handle = {
authenticateUser: authenticateUserHandler,
registerUser: registerUserHandler
}

export default handle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
26 changes: 26 additions & 0 deletions staff/marti-herms/project/V-HUB/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dotenv/config'
import express from 'express'

import { mongoose } from 'core'

import { cors, jsonBodyParser, jwtVerifier, errorHandler } from './middleware'

import handle from './handlers'

mongoose.connect(process.env.MONGODB_URI)
.then(() => {
console.info(`API connected to ${process.env.MONGODB_URI}`)

const api = express()

api.use(cors)

api.post('/users', jsonBodyParser, handle.registerUser)

api.post('/users/auth', jsonBodyParser, handle.authenticateUser)

api.use(errorHandler)

api.listen(process.env.PORT, () => console.info(`API listening on PORT ${process.env.PORT}`))
})
.catch(error => console.error(error))
7 changes: 7 additions & 0 deletions staff/marti-herms/project/V-HUB/api/middleware/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', '*')
res.setHeader('Access-Control-Allow-Methods', '*')

next()
}
24 changes: 24 additions & 0 deletions staff/marti-herms/project/V-HUB/api/middleware/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { errors } from 'com'

const { NotFoundError, CredentialsError, DuplicityError, SessionError, ValidationError } = errors

export default (error, req, res, next) => {
let status = 500

if (error instanceof NotFoundError)
status = 404

else if (error instanceof CredentialsError || error instanceof DuplicityError)
ststus = 409

else if (error instanceof OwnershipError)
status = 403

else if (error instanceof ValidationError)
status = 400

else if (error instanceof SessionError)
status = 498

res.status(status).json({ error: error.constructor.name, message: error.message })
}
11 changes: 11 additions & 0 deletions staff/marti-herms/project/V-HUB/api/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cors from './cors.js'
import jsonBodyParser from './jsonBodyParser.js'
import jwtVerifier from './jwtVerifier.js'
import errorHandler from './errorHandler.js'

export {
cors,
jsonBodyParser,
jwtVerifier,
errorHandler
}
11 changes: 11 additions & 0 deletions staff/marti-herms/project/V-HUB/api/middleware/jsonBodyParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (req, res, next) => {
req.setEncoding('utf-8')

req.on('data', json => {
const body = JSON.parse(json)

req.body = body

next()
})
}
26 changes: 26 additions & 0 deletions staff/marti-herms/project/V-HUB/api/middleware/jwtVerifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dotenv/config'
import jwt from 'jsonwebtoken'

import { errors } from 'com'

const { SessionError } = errors

export default (req, res, next) => {
const { authorization } = req.headers

const token = authorization.slice(7)

jwt.verifier(token, process.env.JWT_SECRET, (error, payload) => {
if (error) {
res.status(498).json({ error: SessionError.name, message: error.message })

return
}

const { sub: userId } = payload

req.userId = userId

next()
})
}
35 changes: 34 additions & 1 deletion staff/marti-herms/project/V-HUB/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion staff/marti-herms/project/V-HUB/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
"main": "index.js",
"type": "module",
"scripts": {
"start": "node .",
"watch": "node --watch .",
"inspect": "node --inspect-brk .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.5",
"com": "file:../com",
"core": "file:../core",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2"
},
"devDependencies": {
"dotenv": "^16.4.5"
}
}
11 changes: 10 additions & 1 deletion staff/marti-herms/project/V-HUB/com/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ class CredentialsError extends Error {
}
}

class SessionError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

const errors = {
ValidationError,
SystemError,
DuplicityError,
NotFoundError,
CredentialsError
CredentialsError,
SessionError
}

export default errors
2 changes: 1 addition & 1 deletion staff/marti-herms/project/V-HUB/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose from 'mongoose'
import logic from './logic'
import data from './data'

export default {
export {
mongoose,
logic,
data
Expand Down
10 changes: 9 additions & 1 deletion staff/marti-herms/project/V-HUB/core/logic/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export default {}
import authenticateUser from './authenticateUser.js'
import registerUser from './registerUser.js'

const logic = {
authenticateUser,
registerUser
}

export default logic

0 comments on commit 69213cf

Please sign in to comment.