# AuthenticationManager

Defined in: [react/src/auth/authentication-manager.ts:73](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L73)

Manages Internet Identity sign-in, session restoration, and authentication
state for a [ClientManager](https://ic-reactor.b3pay.net/v3/libs/classes/clientmanager/).

## Example

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

## Constructors

### Constructor

> **new AuthenticationManager**(`__namedParameters`): `AuthenticationManager`

Defined in: [react/src/auth/authentication-manager.ts:127](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L127)

#### Parameters

##### \_\_namedParameters

[`AuthenticationManagerParameters`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authenticationmanagerparameters/)

#### Returns

`AuthenticationManager`

## Properties

### clientManager

> `readonly` **clientManager**: [`ClientManager`](https://ic-reactor.b3pay.net/v3/libs/classes/clientmanager/)

Defined in: [react/src/auth/authentication-manager.ts:120](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L120)

## Accessors

### authState

#### Get Signature

> **get** **authState**(): [`AuthState`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authstate/)

Defined in: [react/src/auth/authentication-manager.ts:123](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L123)

The current authentication state.

##### Returns

[`AuthState`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authstate/)

## Methods

### dispose()

> **dispose**(): `void`

Defined in: [react/src/auth/authentication-manager.ts:283](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L283)

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](#prepareclient),
[login](#login), [logout](#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](#prepareclient), [login](#login) or
[authenticate](#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

`void`

#### Example

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

***

### subscribeAuthState()

> **subscribeAuthState**(`callback`): () => `void`

Defined in: [react/src/auth/authentication-manager.ts:308](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L308)

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

##### callback

(`state`) => `void`

Function called with the new state.

#### Returns

An unsubscribe function.

() => `void`

***

### prepareClient()

> **prepareClient**(`options?`): `Promise`\<[`AuthClientLike`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authclientlike/) \| `undefined`\>

Defined in: [react/src/auth/authentication-manager.ts:330](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L330)

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

Call (and await) this before wiring up a login button: it makes
[login](#login) and [IdentityAttributesManager.request](https://ic-reactor.b3pay.net/v3/libs/classes/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

##### options?

[`AuthenticationClientOptions`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authenticationclientoptions/)

#### Returns

`Promise`\<[`AuthClientLike`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authclientlike/) \| `undefined`\>

***

### getPreparedClient()

> **getPreparedClient**(`options?`): [`AuthClientLike`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authclientlike/) \| `undefined`

Defined in: [react/src/auth/authentication-manager.ts:400](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L400)

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

##### options?

[`AuthenticationClientOptions`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authenticationclientoptions/)

#### Returns

[`AuthClientLike`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authclientlike/) \| `undefined`

***

### authenticate()

> **authenticate**(): `Promise`\<`Identity` \| `undefined`\>

Defined in: [react/src/auth/authentication-manager.ts:406](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L406)

#### Returns

`Promise`\<`Identity` \| `undefined`\>

***

### login()

> **login**(`loginOptions?`): `Promise`\<`void`\>

Defined in: [react/src/auth/authentication-manager.ts:548](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L548)

#### Parameters

##### loginOptions?

[`AuthenticationSignInOptions`](https://ic-reactor.b3pay.net/v3/libs/interfaces/authenticationsigninoptions/)

#### Returns

`Promise`\<`void`\>

***

### logout()

> **logout**(`options?`): `Promise`\<`void`\>

Defined in: [react/src/auth/authentication-manager.ts:609](https://github.com/B3Pay/ic-reactor/blob/ad82a03d03149c25f0d0f63e453f62e284c28b02/packages/react/src/auth/authentication-manager.ts#L609)

#### Parameters

##### options?

###### returnTo?

`string`

#### Returns

`Promise`\<`void`\>