You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TSha2KeyRandSessionIdGeneratorFactory.create('your very secret hush hush key')
223
223
);
224
224
```
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.
225
233
226
234
## Create dispatcher instance which support session
227
235
@@ -250,6 +258,38 @@ container.add(
250
258
```
251
259
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).
252
260
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
+
253
293
## Injecting session manager instance to controller or middleware
0 commit comments