AuthenticationManager
Defined in: react/src/auth/authentication-manager.ts:73
Manages Internet Identity sign-in, session restoration, and authentication state for a ClientManager.
Example
Section titled “Example”const authentication = new AuthenticationManager({ clientManager })const identity = await authentication.authenticate()Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AuthenticationManager(
__namedParameters):AuthenticationManager
Defined in: react/src/auth/authentication-manager.ts:127
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”AuthenticationManagerParameters
Returns
Section titled “Returns”AuthenticationManager
Properties
Section titled “Properties”clientManager
Section titled “clientManager”
readonlyclientManager:ClientManager
Defined in: react/src/auth/authentication-manager.ts:120
Accessors
Section titled “Accessors”authState
Section titled “authState”Get Signature
Section titled “Get Signature”get authState():
AuthState
Defined in: react/src/auth/authentication-manager.ts:123
The current authentication state.
Returns
Section titled “Returns”Methods
Section titled “Methods”dispose()
Section titled “dispose()”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.
Returns
Section titled “Returns”void
Example
Section titled “Example”const [authentication] = useState( () => new AuthenticationManager({ clientManager }))useEffect(() => () => authentication.dispose(), [authentication])subscribeAuthState()
Section titled “subscribeAuthState()”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.
Parameters
Section titled “Parameters”callback
Section titled “callback”(state) => void
Function called with the new state.
Returns
Section titled “Returns”An unsubscribe function.
() => void
prepareClient()
Section titled “prepareClient()”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.
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”Promise<AuthClientLike | undefined>
getPreparedClient()
Section titled “getPreparedClient()”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.
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”AuthClientLike | undefined
authenticate()
Section titled “authenticate()”authenticate():
Promise<Identity|undefined>
Defined in: react/src/auth/authentication-manager.ts:406
Returns
Section titled “Returns”Promise<Identity | undefined>
login()
Section titled “login()”login(
loginOptions?):Promise<void>
Defined in: react/src/auth/authentication-manager.ts:548
Parameters
Section titled “Parameters”loginOptions?
Section titled “loginOptions?”Returns
Section titled “Returns”Promise<void>
logout()
Section titled “logout()”logout(
options?):Promise<void>
Defined in: react/src/auth/authentication-manager.ts:609
Parameters
Section titled “Parameters”options?
Section titled “options?”returnTo?
Section titled “returnTo?”string
Returns
Section titled “Returns”Promise<void>