Skip to content

Instantly share code, notes, and snippets.

@lrazovic
Last active February 8, 2022 16:22
Show Gist options
  • Save lrazovic/cafe4e7eea380287a9975f8b916cfb8d to your computer and use it in GitHub Desktop.
Save lrazovic/cafe4e7eea380287a9975f8b916cfb8d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function send() {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4) {
console.log(req.response);
if (req.response["result"] === true) {
window.localStorage.setItem("jwt", req.response["access_token"]);
window.localStorage.setItem(
"refresh",
req.response["refresh_token"]
);
}
}
};
req.withCredentials = true;
req.responseType = "json";
req.open(
"get",
"/auth/token?" + window.location.search.substr(1),
true
);
req.send("");
}
</script>
<button onClick="send()">Get FastAPI JWT Token</button>
<button onClick='fetch("dummy/getnews").then(
(r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Unprotected API
</button>
<button onClick='fetch("dummy/prot/summary").then(
(r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Protected API without JWT
</button>
<button onClick='fetch("dummy/prot/summary",{
headers:{
"Authorization": "Bearer " + window.localStorage.getItem("jwt")
},
}).then((r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Protected API wit JWT
</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment