forked from b00tc4mp/isdi-bootcamp-202409
-
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.
- Loading branch information
Showing
23 changed files
with
6,243 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
staff/rafael-infante/project/api/helpers/authorizationHandler.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,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)) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
staff/rafael-infante/project/api/helpers/createFunctionalHandler.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,9 @@ | ||
export default callback => | ||
(req, res, next) => { | ||
try { | ||
callback(req, res) | ||
.catch(next) | ||
} catch (error) { | ||
next(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,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) | ||
} |
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 createFunctionalHandler from './createFunctionalHandler.js' | ||
import authorizationHandler from './authorizationHandler.js' | ||
import errorHandler from './errorHandler.js' | ||
|
||
export { | ||
createFunctionalHandler, | ||
authorizationHandler, | ||
errorHandler | ||
} |
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,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.
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 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
17
staff/rafael-infante/project/api/logic/registerUser.test.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,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()) |
Oops, something went wrong.