set method

void set(
  1. String name,
  2. String value, {
  3. String? domain,
  4. String? path = '/',
  5. DateTime? expires,
  6. Duration? maxAge,
  7. bool httpOnly = false,
  8. bool secure = false,
  9. String? sameSite,
})

Sets a cookie in the response.

Implementation

void set(
  String name,
  String value, {
  String? domain,
  String? path = '/',
  DateTime? expires,
  Duration? maxAge,
  bool httpOnly = false,
  bool secure = false,
  String? sameSite,
}) {
  if (_response == null) return;

  final cookie = Cookie(name, value);

  if (domain != null) cookie.domain = domain;
  if (path != null) cookie.path = path;
  if (expires != null) cookie.expires = expires;
  if (maxAge != null) cookie.maxAge = maxAge.inSeconds;
  cookie.httpOnly = httpOnly;
  cookie.secure = secure;

  if (sameSite != null) {
    switch (sameSite.toLowerCase()) {
      case 'strict':
        cookie.sameSite = SameSite.strict;
        break;
      case 'lax':
        cookie.sameSite = SameSite.lax;
        break;
      default:
        // Keep default
        break;
    }
  }

  _response.cookies.add(cookie);
}