Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Fix/radu/auth middleware #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const HttpStatus = require('http-status-codes')
const { decodeTkn, getEvenToken, constants } = require('../utils')
const { idClaim } = constants
const payloadValidation = require('./payloadValidation')
const upload = require('./uploadValidation')

Expand Down Expand Up @@ -40,42 +38,5 @@ exports.requireAdmin = (req, res, next) => {
next()
}

exports.requireAuth = () => {
const skipPaths = ['/auth/register', '/auth/login']

return (req, res, next) => {
if (skipPaths.includes(req.path)) {
return next()
}

try {
const token = getEvenToken(req)

if (token) {
const decoded = decodeTkn(token, req.config.JWT_KEY)

if (!decoded[idClaim]) {
return res.status(HttpStatus.BAD_REQUEST).json({
success: false,
message: 'You must have an idClaim in your token'
})
}

req.user = decoded
return next()
}
return res.status(HttpStatus.UNAUTHORIZED).json({
success: false,
message: 'You must have an authorization token'
})
} catch (error) {
return res.status(HttpStatus.UNAUTHORIZED).json({
success: false,
message: error.message
})
}
}
}

exports.payloadValidation = payloadValidation
exports.upload = upload
Loading