From 0c0e4e99461d7b67dfa0ddf74f15f92fe7ca99f7 Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Tue, 21 Nov 2023 17:45:50 +0000 Subject: [PATCH] fix: :bug: failed authenticated refreshes will now log the user out --- .changeset/wet-trains-fry.md | 5 +++++ src/pocketbase.ts | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .changeset/wet-trains-fry.md diff --git a/.changeset/wet-trains-fry.md b/.changeset/wet-trains-fry.md new file mode 100644 index 0000000..c64b2d3 --- /dev/null +++ b/.changeset/wet-trains-fry.md @@ -0,0 +1,5 @@ +--- +"forager": patch +--- + +failed authenticated refreshes will now log the user out diff --git a/src/pocketbase.ts b/src/pocketbase.ts index 4ac168d..0611b63 100644 --- a/src/pocketbase.ts +++ b/src/pocketbase.ts @@ -4,6 +4,23 @@ import type { ItemsRecordWithID, LandmarksRecordWithID, UserRecordWithID } from export const isError = (err: unknown): err is Error => err instanceof Error + +interface AuthError { + code: number + message: string + data: AuthErrorData +} + +interface AuthErrorData { + code: number + message: string + data: Record +} + +function isAuthError(err: unknown): err is AuthError { + return (err as AuthError).data.message === 'The request requires valid record authorization token to be set.' && (err as AuthError).data.code === 401 +} + const state = useStorage('forager-store', { server: import.meta.env.VITE_POCKETBASE_URL, }) @@ -40,6 +57,7 @@ export const usePocketBase = defineStore('pocketbase-store', () => { health.value = response } catch (error: unknown) { + if (isError(error)) setErrorMessage(error) health.value = undefined @@ -56,6 +74,7 @@ export const usePocketBase = defineStore('pocketbase-store', () => { return 'success' } catch (error: unknown) { + if (isError(error)) setErrorMessage(error) } @@ -132,8 +151,12 @@ export const usePocketBase = defineStore('pocketbase-store', () => { try { await pb.collection('users').authRefresh() } + catch (error: unknown) { - if (isError(error)) + if (isError(error) && isAuthError(error)) + pb.authStore.clear() + + if (isError(error)) setErrorMessage(error) } }