Skip to content

Commit

Permalink
fix: 🐛 failed authenticated refreshes will now log the user out
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Nov 21, 2023
1 parent f03137f commit 0c0e4e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/wet-trains-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"forager": patch
---

failed authenticated refreshes will now log the user out
25 changes: 24 additions & 1 deletion src/pocketbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
}

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,
})
Expand Down Expand Up @@ -40,6 +57,7 @@ export const usePocketBase = defineStore('pocketbase-store', () => {
health.value = response
}
catch (error: unknown) {

if (isError(error))
setErrorMessage(error)
health.value = undefined
Expand All @@ -56,6 +74,7 @@ export const usePocketBase = defineStore('pocketbase-store', () => {
return 'success'
}
catch (error: unknown) {

if (isError(error))
setErrorMessage(error)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 0c0e4e9

Please sign in to comment.