Basic auth with axios — NodeJS & browser

Hamza Sabljakovic
1 min readJul 20, 2020

--

Door lock
Photo by iMattSmart on Unsplash

Axios, like many other popular HTTP clients, has built-in support for basic auth. However, very often in code reviews, and while browsing stack overflow answers I run into implementations similar to this:

Formatting and base64 encoding username & password

While there is nothing wrong with the code itself, there is a simpler way to achieve the same thing.

Using axios api for a basic auth

As you can see, the axios provides an elegant way to invoke APIs protected with basic auth, hiding away the base64 encoding and string concatenation.

--

--