From ffed1e80af0c50cc9fdedbea6f49ff11ee551a83 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Sun, 9 Jun 2024 21:46:47 +0100 Subject: [PATCH] fix: redirect to /login if can't fetch user profile (#4) Co-authored-by: Adeeb Shihadeh --- src/api/auth/client.ts | 5 +---- src/api/profile.ts | 5 +++++ src/pages/dashboard/Dashboard.tsx | 6 ++++++ 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 src/api/profile.ts diff --git a/src/api/auth/client.ts b/src/api/auth/client.ts index 6a433445..80796ee7 100644 --- a/src/api/auth/client.ts +++ b/src/api/auth/client.ts @@ -45,10 +45,7 @@ export function setAccessToken(token: string): void { } export function clearAccessToken(): void { - if (typeof window === 'undefined') { - return - } - window.localStorage.removeItem(AUTH_KEY) + setAccessToken('') } export function isSignedIn(): boolean { diff --git a/src/api/profile.ts b/src/api/profile.ts new file mode 100644 index 00000000..3ce81a45 --- /dev/null +++ b/src/api/profile.ts @@ -0,0 +1,5 @@ +import type { Profile } from '~/types' + +import { fetcher } from '.' + +export const getProfile = async () => fetcher('/v1/me/') diff --git a/src/pages/dashboard/Dashboard.tsx b/src/pages/dashboard/Dashboard.tsx index be461b80..58073311 100644 --- a/src/pages/dashboard/Dashboard.tsx +++ b/src/pages/dashboard/Dashboard.tsx @@ -12,6 +12,7 @@ import type { VoidComponent } from 'solid-js' import { Navigate, useLocation } from 'solid-start' import { getDevices } from '~/api/devices' +import { getProfile } from '~/api/profile' import type { Device } from '~/types' import Drawer from '~/components/material/Drawer' @@ -66,6 +67,8 @@ const DashboardLayout: VoidComponent = () => { const toggleDrawer = () => setDrawer((prev) => !prev) const [devices] = createResource(getDevices) + const [profile] = createResource(getProfile) + return ( { } > + + +