2024-09-24 12:10:18 +00:00
|
|
|
const CustomError = require('../errors')
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
const authorization = async (req, res, next) => {
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
// next()
|
|
|
|
|
|
|
|
const authHeader = req.headers.authorization
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
if (!authHeader) {
|
|
|
|
throw new CustomError.BadRequestError('Authorization not found into header!')
|
|
|
|
}
|
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
const [, token] = authHeader.split(" ")
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
throw new CustomError.BadRequestError('Authorization token not found into header!')
|
|
|
|
}
|
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
if (token != process.env.TOKEN) {
|
2023-11-29 20:05:48 +00:00
|
|
|
throw new CustomError.UnauthorizedError('Authorization token Invalid')
|
|
|
|
}
|
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
next()
|
|
|
|
|
|
|
|
}
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2024-09-24 12:10:18 +00:00
|
|
|
authorization,
|
2023-11-29 20:05:48 +00:00
|
|
|
}
|