session property

Future<Session?> get session

Implementation

Future<Session?> get session async {
  try {
    if (!await tokenFile.exists()) {
      return null;
    }

    final session = Session.fromJson(
      jsonDecode(
        await tokenFile.readAsString(),
      ),
    );

    final expiryDate = Jwt.getExpiryDate(session.idToken);

    final diff = expiryDate!.difference(DateTime.now()).inSeconds;

    if (diff >= 30) {
      return session;
    }

    /// token will expire soon
    /// it must be refreshed
    final result = await _refreshToken(session);
    return result.when(
      success: (_, session) async {
        await saveSession(session);
        return session;
      },
      failed: (_, __) async => null,
    );
  } catch (_) {
    return null;
  }
}