Skip to content

Commit

Permalink
fix: redirect to /login if can't fetch user profile (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Adeeb Shihadeh <[email protected]>
  • Loading branch information
incognitojam and adeebshihadeh authored Jun 9, 2024
1 parent 877a22f commit ffed1e8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/api/auth/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/api/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Profile } from '~/types'

import { fetcher } from '.'

export const getProfile = async () => fetcher<Profile>('/v1/me/')
6 changes: 6 additions & 0 deletions src/pages/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -66,6 +67,8 @@ const DashboardLayout: VoidComponent = () => {
const toggleDrawer = () => setDrawer((prev) => !prev)

const [devices] = createResource(getDevices)
const [profile] = createResource(getProfile)

return (
<DashboardContext.Provider value={{ drawer, setDrawer, toggleDrawer }}>
<Drawer
Expand All @@ -85,6 +88,9 @@ const DashboardLayout: VoidComponent = () => {
</>
}
>
<Match when={profile.error}>
<Navigate href="/login" />
</Match>
<Match when={dateStr()} keyed>
<RouteActivity dongleId={dongleId()} dateStr={dateStr()} />
</Match>
Expand Down

0 comments on commit ffed1e8

Please sign in to comment.