Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): refresh token early #922

Merged
merged 18 commits into from
Aug 22, 2024
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