forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement middlewares in api b00tc4mp#84
- Loading branch information
Showing
15 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules |
1 change: 1 addition & 0 deletions
1
staff/marti-herms/project/V-HUB/api/handlers/authenticateUserHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
staff/marti-herms/project/V-HUB/api/handlers/registerUserHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
staff/marti-herms/project/V-HUB/api/middleware/errorHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
staff/marti-herms/project/V-HUB/api/middleware/jsonBodyParser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
staff/marti-herms/project/V-HUB/api/middleware/jwtVerifier.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |