addCookie method

dynamic addCookie(
  1. String key,
  2. String value, {
  3. Duration? duration,
  4. bool safe = true,
})

Adds or updates a cookie in the response.

Optionally encrypts the cookie value if safe is true.

key - The name of the cookie. value - The value of the cookie. duration - The duration for which the cookie should be valid. Default is null. safe - A flag indicating whether to encrypt the cookie value. Default is true.

Implementation

addCookie(
  String key,
  String value, {
  Duration? duration,
  bool safe = true,
}) {
  cookies.removeWhere((element) => element.name == key);
  value = safe ? value.toSafe(WaServer.config.cookiePassword) : value;
  var cookie = Cookie(key, value);
  cookie.maxAge = duration?.inSeconds;
  cookie.path = '/';
  cookie.secure = false;
  cookie.httpOnly = false;
  _rq.response.cookies.add(cookie);
  _rq.cookies.add(cookie);
}