3

So, I have a rest api developed in Express. For authentication, I'm using cookies and to fetch user info I just do a get request to an endpoint that return me user info if its logged in or a 401 (Unauthorized) status code if its not. My concern is about, when I get a 401 status code, the chrome developer console print

Failed to load resource: the server responded with a status of 401 (Unauthorized)

It does not cause any bug in the client, just that It bothers me to see it hah.

  • 1
    Genuinely important question: why does it bother you? It's in your dev console, not in a user-facing part of the site, so you only see it (and you SHOULD see it) while inspecting the site as a developer or when reporting problems. Also, 401 is for if the user's credentials don't have the access level necessary to access a resource. If you're responding to an anonymous/not-logged-in request, that should be a 403, not a 401. – Mike 'Pomax' Kamermans Jun 28 '18 at 16:36
  • @Mike'Pomax'Kamermans It's just that I feel good when I see the console clean, just personal taste but you are right. Also, I thought 401 was the correct one in that case but it's true, I should use 403, thank you. – Leonardo Emilio Dominguez Jun 28 '18 at 16:38
  • 1
    If you don't want to see the error in the console, the simplest solution is to not generate it: for instance, make your "is user logged in?" route return some JSON that represents a status object for the user's login status, with a normal HTTP 200 response, and then check that on the JS side to see if you should continue or not. – Mike 'Pomax' Kamermans Jun 28 '18 at 16:40
  • You are right but I will just keep status code, I just thought there was a way to hide that but if not, I can handle it. I think it's a good practice to have descriptive status codes. – Leonardo Emilio Dominguez Jun 28 '18 at 16:43

1 Answers1

3

Create an interceptor in the HTTP requests and upon received response, use the single line code console.clear(); to clear the console output.

In that way, even if you receive 401 or 403 or any response from server and a console error/warning is auto generated, then it will be auto cleared as well!

Ravi Maniyar
  • 661
  • 2
  • 7
  • 22