[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/flow](../../README) / [index](../README) / session

# Function: session()

## Call Signature

> **session**(`value`, `context`): `void`

Defined in: [flow/src/decorators.ts:964](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/decorators.ts#L964)

Binds a field to the server-side session, so its value survives across page loads.

### Parameters

#### value

`unknown`

#### context

`ClassFieldDecoratorContext`

### Returns

`void`

When configured, a field decorator; when used bare, nothing (applied directly).

### Remarks

Requires `SessionMiddleware`. The field reads and writes the session's own key of the same
name, so `@session userId` is the same `userId` a controller sees — pass `{ key }` to read a
differently-named one, or `{ scoped: true }` to namespace it to this component instead.

The value stays **server-side**: `@session` alone puts nothing in the snapshot, so the
browser never sees it and cannot write it. Add `@locked` for a value the client should read.
Applies to a field only.

### Example

```tsx
class Preferences extends Component {
  @session userId = "";                          // the session's `userId`
  @session({ key: "s" }) whatever = 0;           // the session's `s`
  @session({ scoped: true }) draft = "";         // flow:Preferences:draft
  @locked @session theme = "light";              // readable by the client, server-owned
}
```

## Call Signature

> **session**(`opts?`): `SessionDecorator`

Defined in: [flow/src/decorators.ts:965](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/decorators.ts#L965)

Binds a field to the server-side session, so its value survives across page loads.

### Parameters

#### opts?

[`SessionOptions`](../interfaces/SessionOptions)

Optional [SessionOptions](../interfaces/SessionOptions) when called with parentheses.

### Returns

`SessionDecorator`

When configured, a field decorator; when used bare, nothing (applied directly).

### Remarks

Requires `SessionMiddleware`. The field reads and writes the session's own key of the same
name, so `@session userId` is the same `userId` a controller sees — pass `{ key }` to read a
differently-named one, or `{ scoped: true }` to namespace it to this component instead.

The value stays **server-side**: `@session` alone puts nothing in the snapshot, so the
browser never sees it and cannot write it. Add `@locked` for a value the client should read.
Applies to a field only.

### Example

```tsx
class Preferences extends Component {
  @session userId = "";                          // the session's `userId`
  @session({ key: "s" }) whatever = 0;           // the session's `s`
  @session({ scoped: true }) draft = "";         // flow:Preferences:draft
  @locked @session theme = "light";              // readable by the client, server-owned
}
```
