ultisuite-client/lib/auth/ensure-access-token.ts
R3D347HR4Y 9ea2d3325d
Some checks are pending
E2E / Playwright e2e (push) Waiting to run
feat(auth): enhance authentication flows with embedded support and UI improvements
- Updated login and signup components to utilize AuthCard for better user experience during redirection.
- Introduced AuthentikEmbedDialog for seamless integration of Authentik's identity portal within the application.
- Enhanced password recovery and signup flows with dynamic theme handling and improved loading states.
- Refactored existing components to streamline authentication processes and improve maintainability.
2026-06-21 00:12:45 +02:00

34 lines
1.1 KiB
TypeScript

import { useAuthStore } from "@/lib/api/auth-store"
import { fetchSession, applySessionToStore } from "@/lib/auth/session-sync"
import { useNativeRuntime } from "@/lib/platform"
import { ensureNativeAccessToken } from "@/lib/auth/native-session"
let syncPromise: Promise<string | null> | null = null
/**
* Resolve the current bearer token.
* - Web: from the httpOnly session cookies (via `/api/auth/session`).
* - Native (Tauri): from the OS secure store, refreshing against Authentik.
*/
export async function ensureAccessToken(): Promise<string | null> {
if (!syncPromise) {
syncPromise = (async () => {
if (useNativeRuntime()) {
return ensureNativeAccessToken()
}
const data = await fetchSession()
if (data && applySessionToStore(data)) {
return useAuthStore.getState().accessToken
}
if (useAuthStore.getState().isAuthenticated()) {
return useAuthStore.getState().accessToken
}
useAuthStore.getState().logout()
return null
})().finally(() => {
syncPromise = null
})
}
return syncPromise
}