# Session

**Kind:** Class

Model class representing the current user's session.

```js
import {useSession} from '@airtable/blocks/ui';

function Username() {
    const session = useSession();

    if (session.currentUser !== null) {
        return <span>The current user's name is {session.currentUser.name}</span>;
    } else {
        return <span>This extension is being viewed in a public share</span>;
    }
}
```

## Properties

### `currentUser`

Type: `CollaboratorData | null`

The current user, or `null` if the extension is running in a publicly shared base.

```js
import {useSession} from '@airtable/blocks/ui';

function CurrentUser() {
    const session = useSession();

    if (!session.currentUser) {
        return <div>This extension is being used in a public share.</div>;
    }

    return <ul>
        <li>ID: {session.currentUser.id}</li>
        <li>E-mail: {session.currentUser.email}</li>
        <li>Name: {session.currentUser.name}</li>
    </ul>;
}
```

### `id`

Type: `string`

The ID for this model.

### `isDeleted`

Type: `boolean`

`true` if the model has been deleted, and `false` otherwise.

In general, it's best to avoid keeping a reference to an object past the
current event loop, since it may be deleted and trying to access any data
of a deleted object (other than its ID) will throw. But if you keep a
reference, you can use `isDeleted` to check that it's safe to access the
model's data.

## Methods

### `checkPermissionsForCreateRecords()`

Checks whether the current user has permission to create any records in the current base. For
more granular permission checks, see `Table.checkPermissionsForCreateRecords`.

Returns `{hasPermission: true}` if the current user can create records,
`{hasPermission: false, reasonDisplayString: string}` otherwise. `reasonDisplayString` may be
used to display an error message to the user.

**Returns:** `PermissionCheckResult`

```js
import {useSession} from '@airtable/blocks/ui';

function CreateButton({onClick}) {
    const session = useSession();
    const updateRecordsCheckResult = session.checkPermissionsForCreateRecords();
    const deniedReason = updateRecordsCheckResult.hasPermission
        ? <span>{updateRecordsCheckResult.reasonDisplayString}</span>
        : null;

    return <div>
        {deniedReason}
        <button onClick={onClick} disabled={!!deniedReason}>
            Create
        </button>
    </div>;
}
```

### `checkPermissionsForDeleteRecords()`

Checks whether the current user has permission to delete any records in the current base. For
more granular permission checks, see `Table.checkPermissionsForDeleteRecords`.

Returns `{hasPermission: true}` if the current user can delete records,
`{hasPermission: false, reasonDisplayString: string}` otherwise. `reasonDisplayString` may be
used to display an error message to the user.

**Returns:** `PermissionCheckResult`

```js
import {useSession} from '@airtable/blocks/ui';

function DeleteButton({onClick}) {
    const session = useSession();
    const updateRecordsCheckResult = session.checkPermissionsForDeleteRecords();
    const deniedReason = updateRecordsCheckResult.hasPermission
        ? <span>{updateRecordsCheckResult.reasonDisplayString}</span>
        : null;

    return <div>
        {deniedReason}
        <button onClick={onClick} disabled={!!deniedReason}>
            Delete
        </button>
    </div>;
```

### `checkPermissionsForUpdateRecords()`

Checks whether the current user has permission to update any records in the current base. For
more granular permission checks, see `Table.checkPermissionsForUpdateRecords`.

Returns `{hasPermission: true}` if the current user can update records,
`{hasPermission: false, reasonDisplayString: string}` otherwise. `reasonDisplayString` may
be used to display an error message to the user.

**Returns:** `PermissionCheckResult`

```js
import {useSession} from '@airtable/blocks/ui';

function UpdateButton({onClick}) {
    const session = useSession();
    const updateRecordsCheckResult = session.checkPermissionsForUpdateRecords();
    const deniedReason = updateRecordsCheckResult.hasPermission
        ? <span>{updateRecordsCheckResult.reasonDisplayString}</span>
        : null;

    return <div>
        {deniedReason}
        <button onClick={onClick} disabled={!!deniedReason}>
            Update
        </button>
    </div>;
}
```

### `hasPermissionToCreateRecords()`

An alias for `session.checkPermissionsForCreateRecords().hasPermission`. For more granular
permission checks, see `Table.hasPermissionToCreateRecords`.

**Returns:** `boolean`

### `hasPermissionToDeleteRecords()`

An alias for `session.checkPermissionsForDeleteRecords().hasPermission`. For more granular
permission checks, see `Table.hasPermissionToDeleteRecords`.

**Returns:** `boolean`

### `hasPermissionToUpdateRecords()`

An alias for `session.checkPermissionsForUpdateRecords().hasPermission`. For more granular
permission checks, see `Table.hasPermissionToUpdateRecords`.

**Returns:** `boolean`

### `toString()`

A string representation of the model for use in debugging.

**Returns:** `string`

### `unwatch(keys, callback, context?)`

Unwatch keys watched with `.watch`.

Should be called with the same arguments given to `.watch`.

Returns the array of keys that were unwatched.

**Parameters:**
- `keys` (`WatchableSessionKey | ReadonlyArray<WatchableSessionKey>`) — the keys to unwatch
- `callback` (`object`) — the function passed to `.watch` for these keys
- `context?` (`FlowAnyObject | null`) — the context that was passed to `.watch` for this `callback`

**Returns:** `Array<WatchableSessionKey>`

### `watch(keys, callback, context?)`

Get notified of changes to the model.

Every call to `.watch` should have a matching call to `.unwatch`.

Returns the array of keys that were watched.

**Parameters:**
- `keys` (`WatchableSessionKey | ReadonlyArray<WatchableSessionKey>`) — the keys to watch
- `callback` (`object`) — a function to call when those keys change
- `context?` (`FlowAnyObject | null`) — an optional context for `this` in `callback`.

**Returns:** `Array<WatchableSessionKey>`
