Sign in pop not coming when using basic-auth with axios

Viewed 188

Just added basic authentication in my node app using basic-auth for specific route. When i try to open it directly through browser browser's signin popup is coming asking for username and password. but when i hit the same API using react axios it is giving me 401 no signin popup coming.

Here is the code from my node app:

  const auth = require("basic-auth");
  app.use((req, res, next) => {
  let user = auth(req);
  if (authRquiredApis.includes(req.url)) {
    if (user === undefined || !isAuthorizedUser(user)) {
      res.statusCode = 401;
      res.setHeader("WWW-Authenticate", 'Basic realm="Node"');
      res.end("Unauthorized");
    } else {
      next();
    }
  } else {
    next();
  }
});

Here is the code for react axios:

 import axios from "axios";

export default axios.create({
  timeout: 120000,
});

and in another file i am making call like:

import { getDataUrl } from "./apiName";
import axios from "./axios";
export const getData = async (data) => {
  return await axios.get(getDataUrl);
};

For testing purpose i have deployed a test api:

https://data.thecyberintel.com/tciserver/testdata Hitting this on browser will ask for authentication enter "admin" "admin" to get response.

https://www.thecyberintel.com/#/test This url internally hitting same api but getting 401

0 Answers
Related