Welcome to the Finch Cookies and Sessions Guide! This guide will walk you through the steps to use cookies and sessions in your Finch application. Whether you're a seasoned developer or just starting, Finch offers a robust set of tools to simplify server-side web app development.

Read or Write Cookies

You can read or write cookies in your Finch application using the rq.getCookie and rq.addCookie methods. Here is an example of how to use them:

app.get(
  path: '/cookie',
  index: (rq) async {
    var value = rq.getCookie('test', def: 'default value');
    rq.addCookie('test', 'new value');
    return rq.renderString(text: 'Cookie value: $value');
  },
);

Safe Cookies

You can encrypt your cookies using the rq.addCookie method. Here is an example of how to use it:

app.get(
  path: '/cookie',
  index: (rq) async {
    var value = rq.getCookie('test', def: 'default value', safe: true);
    rq.addCookie('test', 'new value', safe: true);
    return rq.renderString(text: 'Cookie value: $value');
  },
);

Read or Write Sessions

You can read or write sessions in your Finch application using the rq.getSession and rq.addSession methods. Here is an example of how to use them:

app.get(
  path: '/session',
  index: (rq) async {
    var value = rq.getSession('test', def: 'default value');
    rq.addSession('test', 'new value');
    return rq.renderString(text: 'Session value: $value');
  },
);