Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: clear local storage after logout

Alaister Youngd0703854791c
diff --git a/studio/components/layouts/AccountLayout/AccountLayout.tsx b/studio/components/layouts/AccountLayout/AccountLayout.tsx
index 29ddcca6914..af70d1570c7 100644
--- a/studio/components/layouts/AccountLayout/AccountLayout.tsx
+++ b/studio/components/layouts/AccountLayout/AccountLayout.tsx
@@ -3,12 +3,11 @@ import { FC, ReactNode } from 'react'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { auth, STORAGE_KEY } from 'lib/gotrue'
+import { useSignOut } from 'lib/auth'
import { useStore, withAuth, useFlag } from 'hooks'
import { IS_PLATFORM } from 'lib/constants'
import WithSidebar from './WithSidebar'
import { SidebarSection } from './AccountLayout.types'
-import { useQueryClient } from '@tanstack/react-query'
interface Props {
title: string
@@ -22,16 +21,15 @@ interface Props {
const AccountLayout: FC<Props> = ({ children, title, breadcrumbs }) => {
const router = useRouter()
const { app, ui } = useStore()
- const queryClient = useQueryClient()
const ongoingIncident = useFlag('ongoingIncident')
const maxHeight = ongoingIncident ? 'calc(100vh - 44px)' : '100vh'
+ const signOut = useSignOut()
const onClickLogout = async () => {
- await auth.signOut()
- localStorage.removeItem(STORAGE_KEY)
+ await signOut()
+
await router.push('/sign-in')
- await queryClient.resetQueries()
}
const organizationsLinks = app.organizations
diff --git a/studio/lib/auth.tsx b/studio/lib/auth.tsx
index 55b17f74866..973530e3afd 100644
--- a/studio/lib/auth.tsx
+++ b/studio/lib/auth.tsx
@@ -1,3 +1,6 @@
+import { useQueryClient } from '@tanstack/react-query'
+import { PropsWithChildren, useCallback, useEffect } from 'react'
+
import {
AuthContext as AuthContextInternal,
AuthProvider as AuthProviderInternal,
@@ -6,9 +9,9 @@ import {
} from 'common'
import { useProfileQuery } from 'data/profile/profile-query'
import { useStore } from 'hooks'
-import { PropsWithChildren, useEffect } from 'react'
-import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
import Telemetry from 'lib/telemetry'
+import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
+import { clearLocalStorage } from './local-storage'
export const AuthContext = AuthContextInternal
@@ -53,3 +56,15 @@ export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
}
export { useAuth, useIsLoggedIn, useSession, useUser } from 'common'
+
+export function useSignOut() {
+ const queryClient = useQueryClient()
+
+ return useCallback(async () => {
+ const result = await gotrueClient.signOut()
+ clearLocalStorage()
+ await queryClient.resetQueries()
+
+ return result
+ }, [])
+}
diff --git a/studio/lib/local-storage.ts b/studio/lib/local-storage.ts
new file mode 100644
index 00000000000..bd2cb0bdcb2
--- /dev/null
+++ b/studio/lib/local-storage.ts
@@ -0,0 +1,9 @@
+export const LOCAL_STORAGE_KEYS_ALLOWLIST = ['graphiql:theme', 'theme']
+
+export function clearLocalStorage() {
+ for (const key in localStorage) {
+ if (!LOCAL_STORAGE_KEYS_ALLOWLIST.includes(key)) {
+ localStorage.removeItem(key)
+ }
+ }
+}
diff --git a/studio/pages/500.tsx b/studio/pages/500.tsx
index b49bc05df1e..e6f43a48713 100644
--- a/studio/pages/500.tsx
+++ b/studio/pages/500.tsx
@@ -5,16 +5,16 @@ import Image from 'next/image'
import { Button } from 'ui'
import { observer } from 'mobx-react-lite'
-import { auth, STORAGE_KEY } from 'lib/gotrue'
+import { useSignOut } from 'lib/auth'
import { useTheme } from 'common'
const Error500: NextPage = () => {
const router = useRouter()
const { isDarkMode } = useTheme()
+ const signOut = useSignOut()
const onClickLogout = async () => {
- await auth.signOut()
- localStorage.removeItem(STORAGE_KEY)
+ await signOut()
await router.push('/sign-in')
router.reload()
}
diff --git a/studio/pages/join/index.tsx b/studio/pages/join/index.tsx
index 6651b06fcaf..a87cffe2e10 100644
--- a/studio/pages/join/index.tsx
+++ b/studio/pages/join/index.tsx
@@ -4,7 +4,7 @@ import { useRouter } from 'next/router'
import { Button, IconCheckSquare, Loading } from 'ui'
import { useStore } from 'hooks'
-import { auth } from 'lib/gotrue'
+import { useSignOut } from 'lib/auth'
import { API_URL } from 'lib/constants'
import { get, post, delete_ } from 'lib/common/fetch'
import { useProfileQuery } from 'data/profile/profile-query'
@@ -25,6 +25,7 @@ const JoinOrganizationPage = () => {
const { slug, token, name } = router.query
const { ui, app } = useStore()
const { data: profile } = useProfileQuery()
+ const signOut = useSignOut()
const [isSubmitting, setIsSubmitting] = useState(false)
const [error, setError] = useState(false)
@@ -135,7 +136,7 @@ const JoinOrganizationPage = () => {
<a
className="cursor-pointer text-brand-900"
onClick={async () => {
- await auth.signOut()
+ await signOut()
router.reload()
}}
>