Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/browser/simple_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/browser/sync/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 16 additions & 11 deletions src/browser/sync/local_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryToken, LocalQuery>;
private readonly queryIdToToken: Map<QueryId, QueryToken>;
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<QueryId>;
private outstandingAuthOlderThanRestart: boolean;
private paused: boolean;
Expand Down Expand Up @@ -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,
Expand Down