Skip to content

Commit

Permalink
change userId by token authorization b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 19, 2024
1 parent bd62c56 commit 7cfa3aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
32 changes: 14 additions & 18 deletions staff/belen-ivars/project/api/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,30 @@ import { errors } from 'com'

const { ContentError, TokenError, CredentialsError, NotFoundError } = errors

export default (req, res) => {
const authenticateUserHandler = async (req, res) => {
const { email, password } = req.body

try {
logic.authenticateUser(email, password)
await logic.authenticateUser(email, password)

.then(userId => {
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXP })
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXP })

res.json(token)
})
.catch(error => {
res.json(token)

let status = 500
if (error instanceof NotFoundError)
status = 404
} catch (error) {
let status = 500

else if (error instanceof CredentialsError)
status = 401
if (error instanceof NotFoundError)
status = 404

res.status(status).json({ error: error.constructor.name, message: error.message })
})
if (error instanceof CredentialsError)
status = 401

} catch (error) {
let status = 500
if (error instanceof ContentError || error instanceof TypeError)
status = 500

status = 406
res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
}

export default authenticateUserHandler
15 changes: 11 additions & 4 deletions staff/belen-ivars/project/api/handlers/createRecipeHandler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { ContentError, NotFoundError } from "com/errors.js";
import logic from "../logic/index.js";
import { errors } from 'com'
import jwt from 'jsonwebtoken'

export default async (req, res) => {
const { author, title, description, image } = req.body
const createRecipeHandler = async (req, res) => {
const token = req.headers.authorization.substring(7)

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

const { title, description, image } = req.body

try {
await logic.createRecipe(author, title, description, image)
await logic.createRecipe(userId, title, description, image)

res.status(201).send()
} catch (error) {
Expand All @@ -18,4 +23,6 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
}

export default createRecipeHandler
5 changes: 3 additions & 2 deletions staff/belen-ivars/project/api/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import logic from '../logic/index.js'
import { errors } from 'com'
const { DuplicityError, ContentError } = errors

export default async (req, res) => {
const registerUserHandler = async (req, res) => {
const { name, email, password } = req.body

try {
Expand All @@ -19,4 +19,5 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
}
export default registerUserHandler
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import jwt from 'jsonwebtoken'
const retrieveRecipesHandler = async (req, res) => {
const token = req.headers.authorization.substring(7)

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

console.log(userId)

try {
Expand Down
10 changes: 7 additions & 3 deletions staff/belen-ivars/project/api/handlers/retrieveUserHandler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ContentError, NotFoundError } from 'com/errors.js'
import logic from '../logic/index.js'
import { validate } from 'com'
import jwt from 'jsonwebtoken'

const complexRetrieve = async (req, res) => {
const userId = req.params.id
const retrieveUserHandler = async (req, res) => {

const token = req.headers.authorization.substring(7)

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

try {
validate.id(userId, 'id')
Expand All @@ -26,4 +30,4 @@ const complexRetrieve = async (req, res) => {
}
}

export default complexRetrieve
export default retrieveUserHandler

0 comments on commit 7cfa3aa

Please sign in to comment.