Skip to content

Commit

Permalink
fix: auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusasferreira committed Dec 2, 2021
1 parent 1a81ffa commit bf59b49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 5 additions & 0 deletions __tests__/unit/ensureAuthenticated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ beforeEach(() => {

describe('Ensure authentication middleware', () => {
it('should respond with 403 status if no headers are provided', () => {

mockRequest = {
headers: {}
}

ensureAuthenticated(mockRequest as Request, mockResponse as Response, nextFunction as NextFunction)

expect(mockResponse.status).toBeCalledWith(403)
Expand Down
8 changes: 3 additions & 5 deletions src/middlewares/ensureAuthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import jwt from 'jsonwebtoken'

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default function ensureAuthenticated(req: Request, res: Response, next: NextFunction){
const {headers} = req

//returns response so tests dont crash when mocking express
if(!headers) return res.status(403).json({message: 'Missing authentication token'})


if(!req.headers['authorization']) return res.status(403).json({message: 'Missing authentication token'})

const [,token] = req.headers['authorization'].split(' ')

jwt.verify(token, process.env.JWT_SECRET, function(err, decoded){
Expand Down

0 comments on commit bf59b49

Please sign in to comment.