Hejka!
Próbuje napisać w expressie serwer który jak narazie ma zarejestrowane następujące funkcje
app.use(bodyParser.json())
app.use(setHeaders)
app.use(authRoutes)
app.use(isAuth)
app.use(usersRoutes)
app.use(errorHandler)
isAuth próbuje sprawdzić w nagłówkach requesta czy jest tam token autoryzacji jednak jeżeli znajduje się po authRoutes to nie moge w nim się dostać do nagłówków przez co wszystkie requesty do usersRoutes mają status 401.
Kod isAuth:
const jwt = require('jsonwebtoken')
const {JWT_SECRET_KEY} = require('../util/constants')
module.exports = (req, res, next) => {
console.log(req.header('Authorization')) //undefined
const token = req.header('Authorization')?.split(' ')[1]
let decodedToken
try {
decodedToken = token && jwt.verify(token, JWT_SECRET_KEY)
}
catch(error) {
error.status = 500
throw error
}
if (!decodedToken) throw {status: 401, message: 'Not authenticated'}
req.userId = decodedToken.userId
next()
}
Wcześniej w authRoutes headery są normalnie widoczne :( jeśli usunę isAuth middleware to dalej w usersRoutes też mam dostęp do nagłówków. Czy ktoś mógłby mi wytłumaczyć dlaczego tak się dzieje? I w jaki sposób do wszystkich routów poza authRoutes dodać middleware sprawdzające uwierzytelnienie użytkownika?