> ## Documentation Index
> Fetch the complete documentation index at: https://stytch.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# On Change Session

> Listen for changes to the current Session using the Stytch Vanilla JS SDK

The Stytch SDK caches the Session of the logged-in User (Represents an end user's account in your application.). Use the `session.onChange` method to listen for and react to changes to the Session object.

## Parameters

**callback** `function`

The callback function to call when the session changes. The first parameter is the updated Session object.

**parameters**

**session** `object`

The updated Session object.

**properties**

**session\_id** `string`

A unique identifier for a specific Session.

**user\_id** `string`

The unique ID of the affected User.

**authentication\_factors** `array[objects]`

An array of different authentication factors that comprise a Session.

**started\_at** `string`

The timestamp when the Session was created. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. 2021-12-29T12:33:09Z.

**last\_accessed\_at** `string`

The timestamp when the Session was last accessed. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. 2021-12-29T12:33:09Z.

**expires\_at** `string`

The timestamp when the Session expires. Values conform to the RFC 3339 standard and are expressed in UTC, e.g. 2021-12-29T12:33:09Z.

**attributes** `object`

Provided attributes help with fraud detection.

**properties**

**ip\_address** `string`

The IP address of the user.

**user\_agent** `string`

The user agent of the User.

**custom\_claims** `map`

The custom claims map for a Session. Claims can be added to a session during a Sessions authenticate call.

**roles** `array[string]`

A list of the roles associated with the session.

## Return value

**unsubscribe** `function`

The function to call to unsubscribe from the Session change event.

```js theme={null}
import { StytchClient } from '@stytch/vanilla-js';

const stytch = new StytchClient('${publicToken}');

const unsubscribe = stytch.session.onChange((session) => {
  if (session) {
    console.log('Session found:', session.session_id);
  } else {
    console.log('No active session');
  }
});

window.addEventListener('beforeunload', () => {
  unsubscribe();
});
```
