parse method

Session parse(
  1. Context ctx
)
override

Parses session from the given request

Implementation

Session parse(Context ctx) {
  String? raw = io.read(ctx);
  if (raw == null) {
    return Session.newSession({});
  }
  Map<String, String>? values = coder.decode(raw);

  if (values == null) {
    return Session.newSession({});
  }

  if (!values.containsKey('sid')) {
    // TODO throw exception?
    return Session.newSession({});
  }
  final String sid = values['sid']!;

  if (!values.containsKey('sct')) {
    // TODO throw exception?
    return Session.newSession({});
  }
  final String timeStr = values['sct']!;
  final int? timeMilli = int.tryParse(timeStr);
  if (timeMilli == null) {
    // TODO throw exception?
    return Session.newSession({});
  }

  final time = DateTime.fromMillisecondsSinceEpoch(timeMilli);

  if (expiry != null) {
    final Duration diff = DateTime.now().difference(time);
    if (diff > expiry!) {
      // TODO throw exception?
      return Session.newSession({});
    }
  }

  return Session(sid, values, time);
}