Skip to content

Commit ac51471

Browse files
committed
add documentation on session middleware
1 parent ff7ca2d commit ac51471

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

working-with-session/index.md

+40
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ sessionMgrFactory := TJsonFileSessionManagerFactory.create(
222222
TSha2KeyRandSessionIdGeneratorFactory.create('your very secret hush hush key')
223223
);
224224
```
225+
## Session initialization and serialization
226+
To be able to keep track of client request, session data needs to be initialized each time request is coming and it needs to be serialized and persisted in storage when response is about to get sent over wire.
227+
228+
There is two mechanism where session initialization and serialization can be done, i.e via dispatcher and middleware.
229+
230+
When using dispatcher for working with session, session is initialized after request object is created and serialized to storage before response is sent to client over the wire. So session is always applied globally through out application.
231+
232+
When using middeware for working with session, session is initialized when middleware is attached globally to application middleware list or per route. Using middleware when working with session is more flexible. For example you may want to apply session management only for certain routes and not other.
225233

226234
## Create dispatcher instance which support session
227235

@@ -250,6 +258,38 @@ container.add(
250258
```
251259
Please read [Dispatcher](/dispatcher) for more information or you may want to get information about [how to create Fano web application project with session using Fano CLI](/scaffolding-with-fano-cli#add-session-support).
252260

261+
## Session middleware
262+
Since v1.10.0, Fano Framework provides session middleware `TSessionMiddleware` which can be use as alternative to using dispatcher when working with session. `TSessionMiddlewareFactory` class is factory class for this middleware.
263+
264+
```
265+
container.add(
266+
'my.session.middleware',
267+
TSessionMiddlewareFactory.create(
268+
container.get('sessionManager') as ISessionManager
269+
)
270+
);
271+
```
272+
273+
To apply session middleware globally so that session is initialized and serialized through out application. If you attach middleware as first middleware to run globally, it is similar compare to when using dispatcher.
274+
275+
```
276+
(container['appMiddlewares'] as IMiddlewareList)
277+
.add(container['my.session.middleware'] as IMiddleware);
278+
```
279+
280+
To apply session middleware so that session is only initialized and serialized when certain routes is accessed, attach middleware to route.
281+
282+
```
283+
router.get('/', container['myHomeCtrl'] as IRequestHandler)
284+
.add(container['my.session.middleware'] as IMiddleware);
285+
router.get('/account', container['myAccountCtrl'] as IRequestHandler)
286+
.add(container['my.session.middleware'] as IMiddleware);
287+
router.get('/staticfile.pdf', container['myFileCtrl'] as IRequestHandler);
288+
289+
```
290+
291+
Code above cause session to be initialized and persisted when route '/' and '/account' is accessed but not when '/staticfile.pdf' route is accessed.
292+
253293
## Injecting session manager instance to controller or middleware
254294

255295
```

0 commit comments

Comments
 (0)