# NylasConnect.onConnectStateChange()

Source: https://developer.nylas.com/docs/v3/auth/nylas-connect/nylasconnect-class/authentication-methods/nylasconnect-onconnectstatechange/

Use the `onConnectStateChange` method to subscribe to authentication state changes.

```ts
nylasConnect.onConnectStateChange((event, session, data) => {
  console.log("Auth state changed:", event);

  switch (event) {
    case "SIGNED_IN":
      console.log("User signed in:", session?.grantInfo?.email);
      break;
    case "CONNECT_ERROR":
      console.error("Connection failed:", data?.error);
      break;
    case "SIGNED_OUT":
      console.log("User signed out");
      break;
  }
});
```

## `ConnectStateChangeCallback`

The state change callback receives comprehensive event information including the event type, session data, and optional event-specific data.

| Property  | Type                    | Description                                                                           |
| --------- | ----------------------- | ------------------------------------------------------------------------------------- |
| `data`    | ConnectEventData\[T]?   | An optional event-specific payload containing additional information about the event. |
| `event`   | ConnectEvent            | The event type.                                                                       |
| `session` | ConnectResult \| `null` | Data about the current session. Returns `null` if no active session is available.     |

### `ConnectEvent`

- **Authentication flow events**
  - `CONNECT_CALLBACK_RECEIVED`: The callback URL was processed.
  - `CONNECT_CANCELLED`: The authentication flow was cancelled.
  - `CONNECT_ERROR`: The authentication flow failed.
  - `CONNECT_POPUP_CLOSED`: The pop-up authentication window closed.
  - `CONNECT_POPUP_OPENED`: The pop-up authentication window opened.
  - `CONNECT_REDIRECT`: The user was redirected to the OAuth provider.
  - `CONNECT_STARTED`: [`NylasConnect.connect()`](/docs/v3/auth/nylas-connect/nylasconnect-class/authentication-methods/nylasconnect-connect/) was called.
  - `CONNECT_SUCCESS`: The authentication flow completed successfully.
- **Session management events**
  - `SESSION_EXPIRED`: A session expired.
  - `SESSION_INVALID`: A session became invalid.
  - `SESSION_RESTORED`: An existing session was found on initialization.
  - `SIGNED_IN`: A grant was authenticated successfully and connected to Nylas.
  - `SIGNED_OUT`: A grant was signed out from Nylas.
- **Token management events**
  - `TOKEN_REFRESHED`: An access token was refreshed successfully.
  - `TOKEN_REFRESH_ERROR`: Token refresh failed.
  - `TOKEN_VALIDATION_ERROR`: Token validation failed.
- **Grant and profile events**
  - `GRANT_UPDATED`: Grant information was updated.
  - `GRANT_PROFILE_LOADED`: Grant profile was fetched from the API.
- **Connection and network events**
  - `CONNECTION_STATUS_CHANGED`: The connection status changed.
  - `NETWORK_ERROR`: A network request failed.
- **Storage events**
  - `STORAGE_CLEARED`: Authentication storage was cleared.
  - `STORAGE_ERROR`: A storage operation failed.