-
Notifications
You must be signed in to change notification settings - Fork 35
Sessions
Not only does Jaguar supports sessions out-of-the-box, it also does the parsing and updating of the session data for you auto-magically.
An object of type Session
holds session data for a particular request. This object for current request can be accessed through session
member of Context
object.
Getting the Session
object of current request:
server.get('/api/add/:item', (ctx) async {
final Session session = await ctx.session;
// ...
});
Session
object provides the subscript operator to access session values by key.
server.get('/api/add/:item', (ctx) async {
final Session session = await ctx.session;
final String oldItem = session['item']; // Get session value for key 'item'
// ...
});
Session
object provides the subscript operator, add
and addAll
method to set/add new data to the session.
server.get('/api/set/:item', (ctx) async {
final Session session = await ctx.session;
session['item'] = ctx.pathParams.item;
// ...
});
remove
methods remove a specific session key, while clear
methods clears all data in the session.
main() async {
final server = new Jaguar();
server.get('/api/add/:item', (ctx) async {
final Session session = await ctx.req.session;
final String newItem = ctx.pathParams.item;
final List<String> items = (session['items'] ?? '').split(',');
// Add item to shopping cart stored on session
if (!items.contains(newItem)) {
items.add(newItem);
session['items'] = items.join(',');
}
return Response.redirect('/');
});
server.get('/api/remove/:item', (ctx) async {
final Session session = await ctx.req.session;
final String newItem = ctx.pathParams.item;
final List<String> items = (session['items'] ?? '').split(',');
// Remove item from shopping cart stored on session
if (items.contains(newItem)) {
items.remove(newItem);
session['items'] = items.join(',');
}
return Response.redirect('/');
});
await server.serve();
}
In the next article, we will explore the session management infrastructure of Jaguar.
Alternatively, readers can jump ahead to Authentication article.
Basics
- Route handler
- Path matching
- Path parameters
- Query parameters
- Serving static files
- Cookies
- Controller
- Parameter binding
- Hot reload
Serialization
Forms
Sessions
Authentication
- Basic authentication
- Form authentication
- JSON authentication
- Authorization
- OAuth
- MongoDb
- PostgreSQL
- MySQL
- Establish connection
- ORM
- Server sent events (SSE)
- Websockets
- systemd
- Docker
- AppEngine