1

I am developing a react application and I have a Authorize.js file which contacts my backend and checks if the user is authorized or not.

AUTHORIZE.JS

import axios from 'axios';

const Authorize = async () => {
  const data = axios
    .get('https://[ENDPOINT]/user/info', {
      headers: {
        'x-jwt-token': localStorage.getItem('x-jwt-token'),
        'x-token': localStorage.getItem('x-token'),
      },
    })
    .then((res) => res.data.data)
    .catch((err) => {
      localStorage.removeItem('x-token');
      localStorage.removeItem('x-jwt-token');
      console.log(err.response);
    });
  return await data;
};

export default Authorize;

Even though I am using .catch at the end I am still getting an error in the console.

Screenshot of the Google Console

How do I remove that xhr.js GET 401 error? I think the .catch() is also working because it is printing the err.response

Any help would be highly appreciated!

  • 1
    Guess you could create an anonymous endpoint for this purpose, one that returns a 200 response with a boolean body. But why does it matter that the 401 is appearing in the console? The console is not part of your app's UI. – Noah Jan 13 '21 at 10:48
  • Sorry, I _thought_ I had the answer, it looks like I'm having a brain fart kinda day... – JO3-W3B-D3V Jan 13 '21 at 11:22
  • @Noah I can only assume that the OP may wanna hide it in the event that someone looks at the console, I mean if you're one of those where you're not technical enough to understand what's going on, but you know about dev tools, etc. It doesn't look very good? IDK, it's a good question, I'm just trying to think of _some_ sort of answer, I mean even with my answer, as long as the app works, 'eh, who cares? I mean even without that, looking at the network tab, you'd be able to see that the request failed... – JO3-W3B-D3V Jan 13 '21 at 11:24

1 Answers1

0

Chrome will log all the network related error messages no matter what.

If this is for development purpose you can disable the network logs in chrome. DevTools->Settings->General->Console->Hide network messages

If this is something you don't want your users to see then in this case you can use console.clear() to clear the console in the catch block.