Some checks are pending
E2E / Playwright e2e (push) Waiting to run
- 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.
34 lines
1.1 KiB
TypeScript
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
|
|
}
|