Do not use like this
The GET method is not good for security
because it exposes your bearer token transmitted as a header to the client.
<html> | |
<head> | |
<title>fetch test</title> | |
</head> | |
<body> | |
<div> | |
<div>session</div> | |
<div id="session"><!-- will be placed here --></div> | |
</div> | |
<script> | |
const host = "https://SECRET.kv.vercel-storage.com" | |
const token = "Y0UR_T0K3N" | |
const cache = (command, key, value) => { | |
const base_url = `${host}/${command}/${key}` | |
if (value) return `${base_url}/${value}` | |
return base_url | |
} | |
const proxy_handler = (url, token) => { | |
return fetch(url, { | |
headers: { | |
Authorization: `Bearer ${token}` | |
} | |
}).then(response => response.json()) | |
} | |
window.onload = function() { | |
var now = Date.now(); | |
const getter = cache("get", "example:test:user_01") | |
const setter = cache("set", "example:test:user_01", now) | |
proxy_handler(getter, token).then(data => { | |
const session = document.getElementById("session"); | |
session.innerHTML = JSON.stringify(data); | |
}) | |
proxy_handler(setter, token) | |
} | |
</script> | |
</body> | |
</html> |