diff --git a/src/browser/simple_client.ts b/src/browser/simple_client.ts index 4269c6e..6a92dc3 100644 --- a/src/browser/simple_client.ts +++ b/src/browser/simple_client.ts @@ -14,6 +14,7 @@ import { import { getFunctionName } from "../server/api.js"; import { AuthTokenFetcher } from "./sync/authentication_manager.js"; import { ConnectionState } from "./sync/client.js"; +import { AuthState } from "./sync/local_state.js"; // In Node.js builds this points to a bundled WebSocket implementation. If no // WebSocket implementation is manually specified or globally available, @@ -254,6 +255,15 @@ export class ConvexClient { return this.client.close(); } + /** + * Get the authentication token to be used for subsequent queries and mutations. + * @returns The authentication state. + */ + getAuth(): AuthState | undefined { + if (this.disabled) return; + return this.client.getAuth(); + } + /** * Set the authentication token to be used for subsequent queries and mutations. * `fetchToken` will be called automatically again if a token expires. diff --git a/src/browser/sync/client.ts b/src/browser/sync/client.ts index cc56332..b5f34b8 100644 --- a/src/browser/sync/client.ts +++ b/src/browser/sync/client.ts @@ -8,7 +8,7 @@ import { logFatalError, Logger, } from "../logging.js"; -import { LocalSyncState } from "./local_state.js"; +import { AuthState, LocalSyncState } from "./local_state.js"; import { RequestManager } from "./request_manager.js"; import { OptimisticLocalStore, @@ -616,6 +616,15 @@ export class BaseConvexClient { return () => this._onTransitionFns.delete(id); } + /** + * Get the authentication token to be used for subsequent queries and mutations. + * @returns The authentication state. + */ + getAuth(): AuthState | undefined { + return this.state.getAuth(); + } + + /** * Set the authentication token to be used for subsequent queries and mutations. * `fetchToken` will be called automatically again if a token expires. diff --git a/src/browser/sync/local_state.ts b/src/browser/sync/local_state.ts index 4cb45e6..116651a 100644 --- a/src/browser/sync/local_state.ts +++ b/src/browser/sync/local_state.ts @@ -27,23 +27,24 @@ type LocalQuery = { componentPath?: string | undefined; }; +export type AuthState = + | { + tokenType: "User"; + value: string; + } + | { + tokenType: "Admin"; + value: string; + impersonating?: UserIdentityAttributes | undefined; + } + export class LocalSyncState { private nextQueryId: QueryId; private querySetVersion: QuerySetVersion; private readonly querySet: Map; private readonly queryIdToToken: Map; private identityVersion: IdentityVersion; - private auth: - | { - tokenType: "User"; - value: string; - } - | { - tokenType: "Admin"; - value: string; - impersonating?: UserIdentityAttributes | undefined; - } - | undefined; + private auth: AuthState | undefined; private readonly outstandingQueriesOlderThanRestart: Set; private outstandingAuthOlderThanRestart: boolean; private paused: boolean; @@ -184,6 +185,10 @@ export class LocalSyncState { return version >= this.identityVersion; } + getAuth(): AuthState | undefined { + return this.auth; + } + setAuth(value: string): Authenticate { this.auth = { tokenType: "User" as const,