Files
Folo/apps/server/client/query/auth.ts
T
DIYgod 9102203956 feat: migrate to better auth (#1951)
* feat: add better-auth

* feat: remove hono/auth-js

* fix: authention check

* fix: signout refetch

* feat: init server auth plugins

* feat: remove types/authjs

* feat: server auth create session plugin

* feat: web login returning

* fix: types

* fix: test env
2024-12-03 12:59:41 +08:00

39 lines
955 B
TypeScript

import { getSession } from "@follow/shared/auth"
import type { AuthSession } from "@follow/shared/hono"
import { useQuery } from "@tanstack/react-query"
import type { FetchError } from "ofetch"
export const useSession = (options?: { enabled?: boolean }) => {
const { data, isLoading, ...rest } = useQuery({
queryKey: ["auth", "session"],
queryFn: () => getSession(),
retry(failureCount, error) {
const fetchError = error as FetchError
if (fetchError.statusCode === undefined) {
return false
}
return !!(3 - failureCount)
},
enabled: options?.enabled ?? true,
meta: {
persist: true,
},
})
const { error } = rest
const fetchError = error as FetchError
return {
session: data?.data as AuthSession,
...rest,
status: isLoading
? "loading"
: data?.data
? "authenticated"
: fetchError
? "error"
: "unauthenticated",
}
}