Skip to content

Instantly share code, notes, and snippets.

@rollcat
Last active May 18, 2017 14:45
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 rollcat/23f6a523dbc4dd2027d4d467647cfea9 to your computer and use it in GitHub Desktop.
Save rollcat/23f6a523dbc4dd2027d4d467647cfea9 to your computer and use it in GitHub Desktop.
Basic auth logout hack
On your backend, create an endpoint (like /api/logout),
that will always respond "401 Unauthorized" (without the WWW-Authenticate header).
On your frontend, include a JS snippet, to create a logout button.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var logout = document.getElementById("logout");
logout.onclick = function() {
xhr = new XMLHttpRequest();
xhr.addEventListener("load", location.reload);
xhr.open("GET", location.origin + "/api/logout");
xhr.send();
};
logout.innerText = "logout";
};
</script>
{% endblock %}
</head>
<body>
<a id="logout"></a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment