Last active
February 15, 2019 16:33
-
-
Save heri16/9a91c0a04bd8a97696fce073a97b4e1d to your computer and use it in GitHub Desktop.
getUser PHP Test Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Ask-The-Expert Test Page</title> | |
<script | |
src="https://cdn.jsdelivr.net/npm/post-robot@9.0.36/dist/post-robot.min.js" | |
integrity="sha256-aHyRkHNH0WAVUfIA8XDrFrnQm/d9GGFeGs0pStD2f7k=" | |
crossorigin="anonymous" | |
></script> | |
</head> | |
<body> | |
<h1>Testing getUser...</h1> | |
<script type="text/javascript"> | |
let currentIdToken = "<?= htmlspecialchars($_REQUEST['token'], ENT_QUOTES, 'UTF-8') ?>"; | |
const refetchPage = idToken => { | |
// Get url of current page | |
var parsedUrl = new URL(window.location.href); | |
// Attach token to the url | |
parsedUrl.searchParams.set('token', idToken); | |
// Replace the current document with the freshly generated page from the server | |
return window | |
.fetch(parsedUrl, { | |
headers: { | |
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
Authorization: `Bearer ${idToken}`, | |
}, | |
}) | |
.then( | |
res => | |
res.ok && (res.headers.get('content-type') || '').includes('text/html') && res.text() | |
) | |
.then(html => { | |
if (html) { | |
document.open('text/html', 'replace'); | |
document.write(html); | |
document.close(); | |
} | |
}); | |
} | |
if (window.parent !== window) { | |
let timer; | |
const client = postRobot.client({ | |
window: window.parent, | |
// domain: 'https://www.equinehub.com' | |
}); | |
// what to do on Logout | |
const onLogout = function() { | |
clearInterval(timer); | |
window.location.href = '/site/logout'; | |
} | |
const checkIdToken = async () => { | |
const data = await client.send('getUser', { logout: onLogout }).then(event => event.data); | |
console.debug('data:', data); | |
if (!data) return null; | |
const { idToken } = data; | |
if (idToken === currentIdToken) return null; | |
currentIdToken = idToken; | |
clearInterval(timer); | |
return refetchPage(idToken); | |
} | |
timer = setInterval(checkIdToken, 5000); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment