0

I've been diving into authentication between Angular and Express, and decided on using token auth with JWTs and the npm jsonwebtoken package. I've got everything set up on the server side and am receiving the token on the client side, but now I need to know how to make it send the token with every request.

From what I've found, most resources out there say to use an $http interceptor to transform every outgoing request. But people at work have always used $httpProvider.headers.defaults.common["Auth"] = token in a .config block, which seems a lot more straightforward to me. Here's a blog explaining how to do it both ways.

But the accepted answer on this stackoverflow post says it would be better to use interceptors, but he doesn't give a reason why.

Any insight would be helpful.

Community
  • 1
  • 1
bobbyz
  • 4,946
  • 3
  • 31
  • 42

1 Answers1

0

After a bunch more research and a conversation on Reddit, it seems like the best way to do it is through the interceptor. Doing the setup in the .config or .run blocks may be good for checking if the user is already authenticated when they first load the app (if there is a token in local storage), but won't be possible for handling dynamic changes like logging out or logging in after the app is loaded. I'm pretty sure you could do it through the $http default headers, but might as well just do it in one place.

Hopefully this helps someone in the future!

bobbyz
  • 4,946
  • 3
  • 31
  • 42