Skip to content

Commit

Permalink
connect api to dat b00tc4mp#237
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafa0297 committed Nov 24, 2024
1 parent 9f33589 commit 350d1e4
Show file tree
Hide file tree
Showing 23 changed files with 6,243 additions and 0 deletions.
18 changes: 18 additions & 0 deletions staff/rafael-infante/project/api/helpers/authorizationHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { errors } from 'com'
import jwt from 'jsonwebtoken'

const { AuthorizationError } = errors

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET)

req.userId = userId

next()
} catch (error) {
next(new AuthorizationError(error.message))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default callback =>
(req, res, next) => {
try {
callback(req, res)
.catch(next)
} catch (error) {
next(error)
}
}
29 changes: 29 additions & 0 deletions staff/rafael-infante/project/api/helpers/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {errors} from "com"

const { ValidationError, SystemError, DuplicityError, CredentialsError, NotFoundError, OwnershipError, AuthorizationError } = errors

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

switch (true) {
case (error instanceof ValidationError):
status = 406
break
case (error instanceof NotFoundError):
status = 404
break
case (error instanceof CredentialsError || error instanceof AuthorizationError):
status = 401
break
case (error instanceof DuplicityError):
status = 409
break
case (error instanceof OwnershipError):
status = 403
break
}

res.status(status).json({ error: status === 500 ? SystemError.name : error.constructor.name, message: error.message })

console.error(error)
}
9 changes: 9 additions & 0 deletions staff/rafael-infante/project/api/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import createFunctionalHandler from './createFunctionalHandler.js'
import authorizationHandler from './authorizationHandler.js'
import errorHandler from './errorHandler.js'

export {
createFunctionalHandler,
authorizationHandler,
errorHandler
}
30 changes: 30 additions & 0 deletions staff/rafael-infante/project/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dotenv/config'
import db from '../dat/index.js'
import express, {json} from 'express'
import cors from 'cors'
import jwt from 'jsonwebtoken'

//import logic from './logic/index.js'
import { createFunctionalHandler, authorizationHandler, errorHandler } from './helpers/index.js'

db.connect(process.env.MONGO_URL_TEST).then(() => {
console.log('connected to db')

const server = express()

server.use(cors())

const jsonBodyParser = json()

server.get('/', (_, res) => res.send('Hello, API!'))

server.post('/users', jsonBodyParser, createFunctionalHandler((req, res) => {
const {name, email, username, password, 'password-repeat': passwordRepeat} = req.body

return
}))

server.use(errorHandler)

server.listen(process.env.PORT, () => console.log(`API listening on port ${process.env.PORT}`))
})
Empty file.
26 changes: 26 additions & 0 deletions staff/rafael-infante/project/api/logic/registerUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import bcrypt from 'bcryptjs'

import {User} from "../../dat/index.js"
import { validate, errors } from 'com'

const {DuplicityError, SystemError} = errors

export default (name, email, username, password, passwordRepeat) => {
validate.name(name)
validate.email(email)
validate.username(username)
validate.password(password)
validate.passwordsMatch(password, passwordRepeat)

return bcrypt.hash(password, 10)
.catch(error => {throw new SystemError(error.message)})
.then(hash =>
User.create({name, email, username, password: hash})
.then(_ => {})
.catch(error => {
if (error.code === 11000) throw new DuplicityError('user already exists')

throw new SystemError(error.message)
})
)
}
17 changes: 17 additions & 0 deletions staff/rafael-infante/project/api/logic/registerUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dotenv/config'
import db from '../../dat/index.js'

import registerUser from './registerUser.js'

db.connect(process.env.MONGO_URL_TEST)
.then(() => {
try {
return registerUser('Rive Lino', '[email protected]', 'rivelino', '123123123', '123123123')
.then(console.log)
.catch(console.error)
} catch (error) {
console.error()
}
})
.catch(console.error)
.finally(() => db.disconnect())
Loading

0 comments on commit 350d1e4

Please sign in to comment.