fix(auth): refresh session cookies on clients
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { setIntegrationIdentify } from "~/initialize/helper"
|
||||
import { useSession } from "~/queries/auth"
|
||||
import { useAuthSessionCookieRefresh, useSession } from "~/queries/auth"
|
||||
|
||||
export const UserProvider = () => {
|
||||
const { session } = useSession()
|
||||
useAuthSessionCookieRefresh(!!session?.user)
|
||||
|
||||
useEffect(() => {
|
||||
if (!session?.user) return
|
||||
|
||||
@@ -4,16 +4,51 @@ import { userSyncService } from "@follow/store/user/store"
|
||||
import { tracker } from "@follow/tracker"
|
||||
import { clearStorage } from "@follow/utils/ns"
|
||||
import type { FetchError } from "ofetch"
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { setLoginModalShow } from "~/atoms/user"
|
||||
import { QUERY_PERSIST_KEY } from "~/constants"
|
||||
import { useAuthQuery } from "~/hooks/common"
|
||||
import { deleteUserCustom as deleteUserFn, getAccountInfo, signOut as signOutFn } from "~/lib/auth"
|
||||
import {
|
||||
deleteUserCustom as deleteUserFn,
|
||||
getAccountInfo,
|
||||
getSession as refreshBetterAuthSession,
|
||||
signOut as signOutFn,
|
||||
} from "~/lib/auth"
|
||||
import { ipcServices } from "~/lib/client"
|
||||
import { clearAuthSessionToken, getAuthSessionToken } from "~/lib/client-session"
|
||||
import { defineQuery } from "~/lib/defineQuery"
|
||||
import { clearLocalPersistStoreData } from "~/store/utils/clear"
|
||||
|
||||
const sessionCookieRefreshInterval = 1000 * 60 * 60 * 12
|
||||
|
||||
let lastSessionCookieRefreshAt = 0
|
||||
let sessionCookieRefreshPromise: Promise<unknown> | null = null
|
||||
|
||||
const refreshAuthSessionCookie = async () => {
|
||||
if (IN_ELECTRON && !getAuthSessionToken()) {
|
||||
return
|
||||
}
|
||||
|
||||
if (Date.now() - lastSessionCookieRefreshAt < sessionCookieRefreshInterval) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionCookieRefreshPromise ??= refreshBetterAuthSession()
|
||||
.then((result) => {
|
||||
if (!result?.error) {
|
||||
lastSessionCookieRefreshAt = Date.now()
|
||||
}
|
||||
return result
|
||||
})
|
||||
.catch(() => null)
|
||||
.finally(() => {
|
||||
sessionCookieRefreshPromise = null
|
||||
})
|
||||
|
||||
await sessionCookieRefreshPromise
|
||||
}
|
||||
|
||||
export const auth = {
|
||||
getSession: () => defineQuery(whoamiQueryKey, () => userSyncService.whoami()),
|
||||
getAccounts: () => defineQuery(["auth", "accounts"], () => getAccountInfo()),
|
||||
@@ -92,6 +127,32 @@ export const useSession = (options?: { enabled?: boolean }) => {
|
||||
} as const
|
||||
}
|
||||
|
||||
export const useAuthSessionCookieRefresh = (enabled: boolean) => {
|
||||
useEffect(() => {
|
||||
if (!enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
const refresh = () => {
|
||||
if (document.visibilityState !== "hidden") {
|
||||
void refreshAuthSessionCookie()
|
||||
}
|
||||
}
|
||||
|
||||
refresh()
|
||||
|
||||
const interval = window.setInterval(refresh, sessionCookieRefreshInterval)
|
||||
window.addEventListener("focus", refresh)
|
||||
document.addEventListener("visibilitychange", refresh)
|
||||
|
||||
return () => {
|
||||
window.clearInterval(interval)
|
||||
window.removeEventListener("focus", refresh)
|
||||
document.removeEventListener("visibilitychange", refresh)
|
||||
}
|
||||
}, [enabled])
|
||||
}
|
||||
|
||||
export const handleSessionChanges = () => {
|
||||
setLoginModalShow(false)
|
||||
const authSessionToken = getAuthSessionToken()
|
||||
|
||||
@@ -12,6 +12,7 @@ import { useIntentHandler } from "./hooks/useIntentHandler"
|
||||
import { useMessaging, useUpdateMessagingToken } from "./hooks/useMessaging"
|
||||
import { useOnboarding } from "./hooks/useOnboarding"
|
||||
import { useUnreadCountBadge } from "./hooks/useUnreadCountBadge"
|
||||
import { useAuthSessionCookieRefresh } from "./lib/auth"
|
||||
import { DebugButton, EnvProfileIndicator } from "./modules/debug"
|
||||
import { ReviewPromptProvider } from "./modules/review-prompt/provider"
|
||||
|
||||
@@ -53,6 +54,7 @@ const ScaleableWrapper: FC<PropsWithChildren> = ({ children }) => {
|
||||
}
|
||||
|
||||
const SideEffect = () => {
|
||||
useAuthSessionCookieRefresh()
|
||||
usePrefetchSessionUser()
|
||||
useUnreadCountBadge()
|
||||
useBackHandler()
|
||||
|
||||
@@ -25,6 +25,7 @@ const storagePrefix = "follow_auth"
|
||||
export const cookieKey = `${storagePrefix}_cookie`
|
||||
export const sessionTokenKey = "__Secure-better-auth.session_token"
|
||||
const sessionDataKey = `${storagePrefix}_session_data`
|
||||
const sessionCookieRefreshIntervalSeconds = 60 * 60 * 12
|
||||
|
||||
let authStateRevision = 0
|
||||
let lastAuthStateChangeAt = 0
|
||||
@@ -115,6 +116,10 @@ const plugins = [
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: `${proxyEnv.API_URL}/better-auth`,
|
||||
sessionOptions: {
|
||||
refetchInterval: sessionCookieRefreshIntervalSeconds,
|
||||
refetchOnWindowFocus: true,
|
||||
},
|
||||
fetchOptions: {
|
||||
cache: "no-store",
|
||||
// Learn more: https://better-fetch.vercel.app/docs/hooks
|
||||
@@ -169,6 +174,11 @@ export const {
|
||||
useSession,
|
||||
} = authClient
|
||||
|
||||
// Mount Better Auth's session atom so the Expo plugin can persist refreshed Set-Cookie metadata.
|
||||
export const useAuthSessionCookieRefresh = () => {
|
||||
useSession()
|
||||
}
|
||||
|
||||
export const forgetPassword = authClient.requestPasswordReset
|
||||
|
||||
export interface AuthProvider {
|
||||
|
||||
Reference in New Issue
Block a user