addCookie method

void addCookie(
  1. String key,
  2. String value, {
  3. Duration? duration,
  4. bool safe = true,
  5. bool secure = false,
  6. bool httpOnly = false,
  7. SameSite? sameSite,
  8. String path = '/',
  9. DateTime? expires,
  10. String? domain,
})

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

void addCookie(
  String key,
  String value, {
  Duration? duration,
  bool safe = true,
  bool secure = false,
  bool httpOnly = false,
  SameSite? sameSite,
  String path = '/',
  DateTime? expires,
  String? domain,
}) {
  key = fixCookieName(key);
  removeCookie(key);
  value = safe ? value.toSafe(FinchApp.config.cookiePassword) : value;
  var cookie = Cookie(key, value);
  cookie.maxAge = duration?.inSeconds;
  cookie.path = path;
  cookie.secure = secure;
  cookie.httpOnly = httpOnly;
  cookie.sameSite = sameSite;
  cookie.expires = expires;
  cookie.domain = domain;
  _rq.response.cookies.add(cookie);
  _rq.cookies.add(cookie);
}