Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active June 14, 2023 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/b34afb4314aa7883834f4f0c266793d6 to your computer and use it in GitHub Desktop.
Save code-boxx/b34afb4314aa7883834f4f0c266793d6 to your computer and use it in GitHub Desktop.
Javascript Fetch With HTTP Basic Auth

JAVASCRIPT FETCH WITH HTTP AUTH

https://code-boxx.com/javascript-fetch-auth/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a protected folder,
    • Move fetch.html, htaccess > .htaccess, htpasswd > .htpasswd inside.
  2. The default user and password is USER and PASS.
  3. Take note that .htaccess only works with Apache.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Fetch Auth</title>
<script>
function fetchAuth () {
// (A) URL & CREDENTIALS
var url = "protected/secret.html",
credentials = btoa("USER:PASS");
// (B) FETCH WITH HTTP AUTH
fetch (url, {
headers: { "Authorization": `Basic ${credentials}` }
})
// (C) SERVER RESPONSE
.then(res => {
if (res.status != 200) { throw new Error("Bad Server Response"); }
return res.text();
})
.then(res => document.getElementById("demo").innerHTML = res)
// (D) HANDLE ERRORS (OPTIONAL)
.catch(err => console.error(err));
}
</script>
</head>
<body>
<div id="demo"></div>
<input type="button" value="Fetch Auth" onclick="fetchAuth()">
</body>
</html>
AuthType Basic
AuthName "Password Required"
AuthUserFile D:\http\protected\.htpasswd
Require valid-user
USER:$apr1$i.0uy9VI$RIL6cpq41G1BEEzsdtY/y0
<p>Wow. Very secret. Such protection. Much covert.</p>
md protected
move secret.html protected
move htaccess protected/.htaccess
move htpasswd protected/.htpasswd
mkdir -m 777 protected
mv ./secret.html ./protected
mv ./htaccess ./protected/.htaccess
mv ./htpasswd ./protected/.htpasswd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment