From aaea0f2b7b3133c4d95872cda6cfcfd15f71a64f Mon Sep 17 00:00:00 2001 From: Andy Tang Date: Wed, 22 May 2024 03:28:28 +0000 Subject: [PATCH] fix signing out globally not working --- src/lib/utils.js | 22 ++++++++++++++++++++-- src/routes/+layout.js | 7 +++++-- src/routes/+layout.svelte | 27 ++++++++++++++++++--------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index 070adbf..e014285 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -67,6 +67,7 @@ export const wordCount = (s) => { return s.split(" ").length; }; +// make sure url is relative export const getSafeRedirect = (url, defaultUrl = "/") => { let result = defaultUrl; @@ -74,8 +75,25 @@ export const getSafeRedirect = (url, defaultUrl = "/") => { result = url; } - let tempURL = new URL(result, "http://localhost"); - tempURL.searchParams.set("reload", "true"); + return addQueryParams(result, { reload: true }); +}; + +// add query param to a relative url +export const addQueryParams = (url = "/", params) => { + let tempURL = new URL(url, "http://localhost"); + + for (const key in params) { + tempURL.searchParams.set(key, params[key]); + } + + return tempURL.pathname + tempURL.search; +}; + +// remove query param from a relative url +export const removeQueryParam = (url = "/", param) => { + let tempURL = new URL(url, "http://localhost"); + + tempURL.searchParams.delete(param); return tempURL.pathname + tempURL.search; }; diff --git a/src/routes/+layout.js b/src/routes/+layout.js index 03e179f..23db1ce 100644 --- a/src/routes/+layout.js +++ b/src/routes/+layout.js @@ -16,15 +16,18 @@ export const load = async ({ fetch, data, depends }) => { data: { session }, } = await supabase.auth.getSession(); + let signedOut = false; + if (session?.user) { session.user = await completeUser(supabase, session.user); // check if user was signed out from somewhere else + // if so, sign out here as well const { data: exists } = await supabase.rpc("session_exists", { session_id: session.user.session_id }); if (!exists) { - await supabase.auth.signOut(); + signedOut = true; } } - return { supabase, session }; + return { supabase, session, signedOut }; }; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 9d51425..d61418a 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -5,12 +5,13 @@ import { onMount } from "svelte"; import { page } from "$app/stores"; import { PUBLIC_RECAPTCHA_SITE_KEY } from "$env/static/public"; + import { removeQueryParam } from "$lib/utils"; import Footer from "./Footer.svelte"; import Header from "./Header.svelte"; import toast, { Toaster } from "svelte-french-toast"; export let data; - $: ({ supabase, session } = data); + $: ({ supabase, session, signedOut } = data); $: pathname = $page.url.pathname.replace(/\/$/, ""); @@ -91,22 +92,30 @@ } } - // if user deleted their account, force sign out - if ($page.url.searchParams.has("signOut")) { + // force sign out in special cases + if ($page.url.searchParams.has("signOut") || signedOut) { document.cookie.split(";").forEach((c) => { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); }); - setTimeout(() => { - location.href = "/"; - }, 5000); + if (signedOut) { + toast("You were signed out from another device.", { + icon: "😔", + duration: 5000, + }); + } + + setTimeout( + () => { + location.href = removeQueryParam(location.href, "signOut"); + }, + signedOut ? 5000 : 0 + ); } // if reload is specified, reload the page if ($page.url.searchParams.has("reload")) { - const url = new URL(location.href); - url.searchParams.delete("reload"); - location.href = url; + location.href = removeQueryParam(location.href, "reload"); } });