Skip to content

Commit

Permalink
feat: refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Dec 25, 2024
1 parent 1126077 commit 97da6b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function DashboardLink() {
queryFn: () => fetchLoginUser(),
refetchOnMount: true,
refetchOnWindowFocus: true,
refetchInterval: 1000 * 60 * 1,
})

return (
Expand Down
9 changes: 9 additions & 0 deletions src/lib/nezha-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LoginUserResponse, MonitorResponse, ServerGroupResponse, ServiceResponse, SettingResponse } from "@/types/nezha-api"

let lastestRefreshTokenAt = 0

export const fetchServerGroup = async (): Promise<ServerGroupResponse> => {
const response = await fetch("/api/v1/server-group")
const data = await response.json()
Expand All @@ -15,6 +17,13 @@ export const fetchLoginUser = async (): Promise<LoginUserResponse> => {
if (data.error) {
throw new Error(data.error)
}

// auto refresh token
if (document.cookie && (!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60)) {
lastestRefreshTokenAt = Date.now()
fetch("/api/v1/refresh-token")
}

return data
}

Expand Down

0 comments on commit 97da6b4

Please sign in to comment.