Skip to content

Commit

Permalink
feat(ui): refresh token early (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrisse authored Aug 22, 2024
1 parent 464ddbd commit 4f0449d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/leapfrogai_ui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,30 @@
import { Toasts } from '$components';
import { page } from '$app/stores';
import '$webComponents/CodeBlock';
import { browser } from '$app/environment';
export let data;
let { supabase, session } = data;
$: ({ supabase, session } = data);
// Refresh token early so backend requests get token with enough time before expiration (for long processing ops)
let startRefreshCountdown = true;
$: {
if (startRefreshCountdown && browser && session) {
startRefreshCountdown = false;
const expiresIn = session.expires_at - Math.floor(Date.now() / 1000); // seconds until expiration
const refreshTime = expiresIn - 1200; // 20 minutes before expiration
if (refreshTime > 0) {
setTimeout(async () => {
await supabase.auth.refreshSession();
startRefreshCountdown = true;
}, refreshTime * 1000);
}
}
}
onMount(() => {
const { data } = supabase.auth.onAuthStateChange((_, newSession) => {
if (newSession?.expires_at !== session?.expires_at) {
Expand Down

0 comments on commit 4f0449d

Please sign in to comment.