Skip to content

Sessions

Ravi Teja Gudapati edited this page Dec 26, 2017 · 11 revisions

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;
    // ...
  });

Accessing session value

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'
    // ...
  });

Updating session value

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.

Example

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();
}

Example projects

  1. Simple example using vanilla Sessions

What's next?

TODO

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally