mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 17:07:47 +02:00
fix(hub): always clear auth store when encountring 4xx after token expires (#2310)
Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
@@ -4,7 +4,7 @@ import { basePath } from "@/components/router"
|
|||||||
import { toast } from "@/components/ui/use-toast"
|
import { toast } from "@/components/ui/use-toast"
|
||||||
import type { ChartTimes, UserSettings } from "@/types"
|
import type { ChartTimes, UserSettings } from "@/types"
|
||||||
import { $alerts, $allSystemsById, $allSystemsByName, $userSettings } from "./stores"
|
import { $alerts, $allSystemsById, $allSystemsByName, $userSettings } from "./stores"
|
||||||
import { chartTimeData } from "./utils"
|
import { chartTimeData, debounce } from "./utils"
|
||||||
|
|
||||||
/** PocketBase JS Client */
|
/** PocketBase JS Client */
|
||||||
export const pb = new PocketBase(basePath)
|
export const pb = new PocketBase(basePath)
|
||||||
@@ -12,7 +12,7 @@ export const pb = new PocketBase(basePath)
|
|||||||
export const isAdmin = () => pb.authStore.record?.role === "admin"
|
export const isAdmin = () => pb.authStore.record?.role === "admin"
|
||||||
export const isReadOnlyUser = () => pb.authStore.record?.role === "readonly"
|
export const isReadOnlyUser = () => pb.authStore.record?.role === "readonly"
|
||||||
|
|
||||||
export const verifyAuth = () => {
|
const verifyAuth = () => {
|
||||||
pb.collection("users")
|
pb.collection("users")
|
||||||
.authRefresh()
|
.authRefresh()
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
@@ -25,6 +25,22 @@ export const verifyAuth = () => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const verifyAuthDebounced = debounce(verifyAuth, 100)
|
||||||
|
|
||||||
|
// verify the session whenever any API request returns a 4xx response (e.g. an
|
||||||
|
// expired JWT). The auth-refresh endpoint is excluded to avoid a loop, since
|
||||||
|
// it returns 401 itself when the token is no longer valid.
|
||||||
|
pb.afterSend = (response, data) => {
|
||||||
|
if (
|
||||||
|
(response.status === 401 || response.status === 403) &&
|
||||||
|
pb.authStore.token &&
|
||||||
|
!response.url.includes("auth-refresh")
|
||||||
|
) {
|
||||||
|
verifyAuthDebounced()
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
/** Logs the user out by clearing the auth store and unsubscribing from realtime updates. */
|
/** Logs the user out by clearing the auth store and unsubscribing from realtime updates. */
|
||||||
export function logOut() {
|
export function logOut() {
|
||||||
$allSystemsByName.set({})
|
$allSystemsByName.set({})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/** biome-ignore-all lint/suspicious/noAssignInExpressions: it's fine :) */
|
/** biome-ignore-all lint/suspicious/noAssignInExpressions: it's fine :) */
|
||||||
import type { PreinitializedMapStore } from "nanostores"
|
import type { PreinitializedMapStore } from "nanostores"
|
||||||
import { pb, verifyAuth } from "@/lib/api"
|
import { pb } from "@/lib/api"
|
||||||
import {
|
import {
|
||||||
$allSystemsById,
|
$allSystemsById,
|
||||||
$allSystemsByName,
|
$allSystemsByName,
|
||||||
@@ -167,11 +167,6 @@ export async function subscribe() {
|
|||||||
export async function refresh() {
|
export async function refresh() {
|
||||||
try {
|
try {
|
||||||
const records = await fetchSystems()
|
const records = await fetchSystems()
|
||||||
if (!records.length) {
|
|
||||||
// No systems found, verify authentication
|
|
||||||
verifyAuth()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
add(record)
|
add(record)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user