loginWithCookie static method

Future<bool> loginWithCookie(
  1. HttpRequest request
)

Creates a new session for the user resolved from the current cookie.

Issues a fresh token without deleting the previous session record. Returns false when no user is resolved or session creation fails.

Implementation

static Future<bool> loginWithCookie(HttpRequest request) async {
  try {
    final authUser = await user(request);

    if (authUser != null) {
      final cookie = Cookie('archery_session', App.generateKey())
        ..httpOnly = true
        ..secure = true
        ..sameSite = SameSite.lax;

      final newAuthSession = await Model.create<AuthSession>(fromJson: {"email": authUser.email, "token": cookie.value});

      if (newAuthSession == null) return false;

      newAuthSession.lastActivity = DateTime.now();
      _cachedSessions?.add(newAuthSession);

      request.response.cookies.add(cookie);
      request.cookies.add(cookie);

      return await newAuthSession.save();
    }

    return false;
  } catch (e) {
    return false;
  }
}