Http auth files blocking the rest of the api

Hello everyone,
I’ve made an http auth api for my frontend. I have a problem were my auth files are requiring authentication for the entire api. This is a problem because it prevents users from creating accounts on my frontend. I haven’t noticed the issue until I deployed it and paired it with my frontend.


^ this is what my server.js file looks like. I’m pretty sure that app.use(basicAuth); and app.use(‘/users’, require(‘./Authentication/users.controller)); are causing the problem. Is there a way to prevent this from requiring authentication for my entire api? Could I dyamnically use app.use(basicAuth); so it only requires authentication on certaim requests/routes.
Thanks for all your help!!

Hi @Liamgrossman, yes sure it would work the same way as you registered the user controller middleware for the /users route only:

app.use('/users', basicAuth)

You might also specify an array for which routes the middleware should be used:

app.use(['/users', '/admin'], basicAuth)

See here for more possibilities.

PS: If with dynamically you mean depending on runtime parameters, you’d have to write your own middleware wrapper though.

This is perfect, thank you!

1 Like