Skip to content
IC Reactor

AuthenticationManager

Defined in: react/src/auth/authentication-manager.ts:73

Manages Internet Identity sign-in, session restoration, and authentication state for a ClientManager.

const authentication = new AuthenticationManager({ clientManager })
const identity = await authentication.authenticate()

new AuthenticationManager(__namedParameters): AuthenticationManager

Defined in: react/src/auth/authentication-manager.ts:127

AuthenticationManagerParameters

AuthenticationManager

readonly clientManager: ClientManager

Defined in: react/src/auth/authentication-manager.ts:120

get authState(): AuthState

Defined in: react/src/auth/authentication-manager.ts:123

The current authentication state.

AuthState

dispose(): void

Defined in: react/src/auth/authentication-manager.ts:283

Releases the @icp-sdk/auth client this manager built.

A v10 client hooks the page when it is built: activity listeners on document, focus and visibility listeners, a watch on the session record that every tab shares and, once signed in, a refresh timer. Nothing releases them when the manager is dropped, so a manager built per mount, as a server-rendered app builds one in its provider, left one live client behind on each remount. Call this when you discard the manager, from the cleanup of whatever built it.

The client is disposed and forgotten, so a later prepareClient, login, logout or restore that needs a client builds a new one. That keeps it safe in an effect cleanup under React’s StrictMode, which runs the cleanup and then the effect again on the same manager. With @icp-sdk/auth v8 there is nothing to release, and the manager only drops its reference. A client passed in as authClient belongs to the caller: it is never disposed, and the manager goes on using it, but stops following its session record until its next prepareClient, login or authenticate, so that the caller’s client does not keep a discarded manager alive.

The auth state and the identity on the agent are left as they are. A restore reading the client when it is released ends without publishing what it read, and a sign-in or sign-out under way ends with what the released client reports. A restore useAuth() started stops, and releases any client it built meanwhile, once no useAuth() of this manager is mounted; one mounted later restores again.

void

const [authentication] = useState(
() => new AuthenticationManager({ clientManager })
)
useEffect(() => () => authentication.dispose(), [authentication])

subscribeAuthState(callback): () => void

Defined in: react/src/auth/authentication-manager.ts:308

Subscribes to auth state changes.

Callbacks run in the order they subscribed, after the state has changed. A callback that throws does not stop the others; the first error is rethrown to whatever made the change once they have all run. When a callback itself changes the state, the newer state is the last each callback hears.

(state) => void

Function called with the new state.

An unsubscribe function.

() => void


prepareClient(options?): Promise<AuthClientLike | undefined>

Defined in: react/src/auth/authentication-manager.ts:330

Preloads the auth module and creates an AuthClient ahead of time.

Call (and await) this before wiring up a login button: it makes login and IdentityAttributesManager.request able to open the identity provider synchronously inside the click handler, which is what browser popup blockers and the ICRC-29 transport require.

AuthenticationClientOptions

Promise<AuthClientLike | undefined>


getPreparedClient(options?): AuthClientLike | undefined

Defined in: react/src/auth/authentication-manager.ts:400

Returns an AuthClient without awaiting, or undefined when the auth module has not been loaded yet.

Callers that must stay inside a user gesture use this instead of ensureClient; awaiting anything before opening the identity provider window loses the gesture.

AuthenticationClientOptions

AuthClientLike | undefined


authenticate(): Promise<Identity | undefined>

Defined in: react/src/auth/authentication-manager.ts:406

Promise<Identity | undefined>


login(loginOptions?): Promise<void>

Defined in: react/src/auth/authentication-manager.ts:548

AuthenticationSignInOptions

Promise<void>


logout(options?): Promise<void>

Defined in: react/src/auth/authentication-manager.ts:609

string

Promise<void>