H
H
Horus1232020-10-29 09:55:30
JavaScript
Horus123, 2020-10-29 09:55:30

How to save user parameters with http basic auth?

I authorize on a remote api, basic auth is configured. Authorization method:

auth() {
const token = Buffer.from(`${email}:${psw}`, 'utf8').toString('base64')
      var config = {
        method: "get",
        url:
          "http://....",
        headers: {
          Authorization: `Basic ${token}`,
        },
      };
      axios(config)
        .then(function (response) {
          console.log(JSON.stringify(response.data));
        })
        .catch(function (error) {
          console.log(error);
        });
    },


When authorizing through a browser through a standard form, the credentials are saved and in the future you can access the server without authorization for some time.
When sending a request, authorization is also successful, but in order to access the same route or another one, the authorization header must be passed again or an error occurs:
Failed to load resource: the server responded with a status of 401 (Unauthorized).
Where can the title be saved? Or how to organize the work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-10-29
@Robur

but to access the same route or another, you need to pass the authorization header again

Yes need. this is what you do.
You can make your own function to call the api, which will substitute the desired header in the request each time.
And already everywhere in the application to use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question