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

***

[Documentation](../../../README) / [@zerotal/session](../README) / SessionManager

# Class: SessionManager

Defined in: [session/src/SessionManager.ts:32](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L32)

Per-request session store — the concrete object behind `ctx.session`.

Created by [SessionMiddleware](SessionMiddleware) at the start of every request (its data
loaded from the configured [SessionDriver](../interfaces/SessionDriver)) and attached to the
`HttpContext`. Exposes a small Map-like API over a plain
`Record<string, unknown>`, plus flash storage and ID regeneration. All
mutations happen in memory; the middleware persists them after the response.

## Remarks

`SessionAccessor` (the `Session` facade / `ctx.session` surface most code
uses) forwards to this class, so the public methods here mirror that API.
Flash handling relies on two internal key-sets: keys flashed in the previous
request are swept after the current response, and keys flashed in the current
request are recorded (under `_flash_keys`) for the next request to sweep.

## Example

```ts
const session = new SessionManager(id, data, driver);
session.set("user_id", 42);
session.flash("status", "Saved!");   // available on the next request only
const status = session.pull("status"); // read once, then removed
```

Do not instantiate directly — use the SessionMiddleware or Session facade.

## Implements

- [`SessionContract`](../../core/contracts/interfaces/SessionContract)

## Constructors

### Constructor

> **new SessionManager**(`id`, `data`, `driver`): `SessionManager`

Defined in: [session/src/SessionManager.ts:45](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L45)

#### Parameters

##### id

`string`

##### data

`Record`\<`string`, `unknown`\>

##### driver

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

#### Returns

`SessionManager`

## Flash data

### pull()

> **pull**(`key`): `unknown`

Defined in: [session/src/SessionManager.ts:81](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L81)

Read the value under `key` and delete it in one step (also clears any flash
marker on that key).

#### Parameters

##### key

`string`

#### Returns

`unknown`

The stored value, or `undefined` if absent.

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`pull`](../../core/contracts/interfaces/SessionContract#pull)

***

### flash()

> **flash**(`key`, `value`): `void`

Defined in: [session/src/SessionManager.ts:134](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L134)

Store a value that survives for exactly ONE subsequent request.

The value is written now and marked so that the next request's
\_prepareForSave sweeps it after that request's response is sent.
Reading it early with [pull](#pull) removes both the value and its marker.
Used internally by `ctx.flash()`.

#### Parameters

##### key

`string`

Key to flash.

##### value

`unknown`

Value to make available on the next request only.

#### Returns

`void`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`flash`](../../core/contracts/interfaces/SessionContract#flash)

## Lifecycle

### regenerate()

> **regenerate**(): `void`

Defined in: [session/src/SessionManager.ts:153](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L153)

Issue a new session ID (keeping the current data), recording the old ID in
\_abandonedIds so the middleware can destroy its server-side record.
Call on privilege changes such as login to prevent session-fixation attacks.

#### Returns

`void`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`regenerate`](../../core/contracts/interfaces/SessionContract#regenerate)

## Reading

### id()

> **id**(): `string`

Defined in: [session/src/SessionManager.ts:63](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L63)

The current session ID.

#### Returns

`string`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`id`](../../core/contracts/interfaces/SessionContract#id)

***

### get()

> **get**(`key`): `unknown`

Defined in: [session/src/SessionManager.ts:71](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L71)

Read the value stored under `key`, or `undefined` if absent.

#### Parameters

##### key

`string`

#### Returns

`unknown`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`get`](../../core/contracts/interfaces/SessionContract#get)

***

### has()

> **has**(`key`): `boolean`

Defined in: [session/src/SessionManager.ts:100](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L100)

Report whether `key` is present in the session data.

#### Parameters

##### key

`string`

#### Returns

`boolean`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`has`](../../core/contracts/interfaces/SessionContract#has)

## Writing

### set()

> **set**(`key`, `value`): `void`

Defined in: [session/src/SessionManager.ts:92](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L92)

Store `value` under `key`, overwriting any existing entry.

#### Parameters

##### key

`string`

##### value

`unknown`

#### Returns

`void`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`set`](../../core/contracts/interfaces/SessionContract#set)

***

### forget()

> **forget**(`key`): `void`

Defined in: [session/src/SessionManager.ts:108](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L108)

Remove a single key from the session (also clears its flash marker).

#### Parameters

##### key

`string`

#### Returns

`void`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`forget`](../../core/contracts/interfaces/SessionContract#forget)

***

### flush()

> **flush**(): `void`

Defined in: [session/src/SessionManager.ts:117](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/session/src/SessionManager.ts#L117)

Discard all session data and pending flash markers. The ID is unchanged.

#### Returns

`void`

#### Implementation of

[`SessionContract`](../../core/contracts/interfaces/SessionContract).[`flush`](../../core/contracts/interfaces/SessionContract#flush)
