Skip to content

Commit

Permalink
Add session status management status checker iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrgn committed Mar 29, 2024
1 parent c307896 commit 29a7206
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/lib/SessionStatus.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script>
function checkSessionStatus() {
var opIframe = document.getElementById('opIframe');
var message = {
type: 'checkSession'
};
// Send message to OP iframe
opIframe.contentWindow.postMessage(message, 'https://keycloak.inform.ubu.dlorigan.dev.cirg.uw.edu');
}
// Poll the OP iframe periodically
setInterval(checkSessionStatus, 5000); // Adjust interval as needed
// Listen for messages from OP iframe
window.addEventListener('message', function(event) {
if (event.origin === 'https://keycloak.inform.ubu.dlorigan.dev.cirg.uw.edu') {
var data = event.data;
if (data && data.type === 'sessionStatus') {
// Session status received from OP iframe
var sessionState = data.sessionState;
if (sessionState === 'logged_out') {
// Update session state in RP accordingly
console.log('User logged out');
// Perform logout actions or redirect to logout page
} else if (sessionState === 'unknown') {
// Update session state in RP accordingly
console.log('User session status: unknown');
// Perform actions based on unknown session status
} else if (sessionState === 'loggedin') {
// Update session state in RP accordingly
console.log('User session status: logged in');
// Perform actions based on logged in session status
} else if (sessionState === 'unchanged') {
// No change in session state on the OP side
console.log('Session state unchanged');
// Perform actions based on unchanged session status
}
}
}
});
</script>

<iframe title="Check Session Status" id="opIframe" src="https://keycloak.inform.ubu.dlorigan.dev.cirg.uw.edu/realms/ltt/protocol/openid-connect/login-status-iframe.html" style="display:none;"></iframe>
9 changes: 4 additions & 5 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import { SOFClient } from '$lib/sofClient';
import { SOF_HOSTS, BACK_URL, INACTIVITY_TIMEOUT } from '$lib/config';
import { goto } from '$app/navigation';
import SessionStatus from '$lib/SessionStatus.svelte';
let shlStore = writable<SHLAdminParams>(undefined);
setContext('shlStore', shlStore);
Expand All @@ -42,11 +44,7 @@
inactivityTimer = setTimeout(logout, INACTIVITY_TIMEOUT);
}
let stateChecker: NodeJS.Timeout = setTimeout(checkState, 60000);
function checkState() {
sofClient.checkState();
stateChecker = setTimeout(checkState, 60000);
}
let stateChecker: NodeJS.Timeout = setInterval(sofClient.checkState, 60000);
onMount(() => {
sofClient.initialize()?.then(() => {
Expand Down Expand Up @@ -110,6 +108,7 @@
</Collapse>
</Navbar>
{#if initialized}
<SessionStatus/>
<Row class="main-row">
<Col>
<slot />
Expand Down

0 comments on commit 29a7206

Please sign in to comment.