Skip to content

Commit

Permalink
chore(ml-opensource#80): Add expiresAt & expiresSoon to authTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
nivisi committed Jan 5, 2023
1 parent d80d25f commit 716cae3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/data/model/auth/auth_tokens.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'auth_tokens.freezed.dart';
part 'auth_tokens.g.dart';

@freezed
class AuthTokens with _$AuthTokens {
Expand All @@ -11,9 +12,21 @@ class AuthTokens with _$AuthTokens {
required String refreshToken,
required String tokenType,
required double expiresIn,
required DateTime expiresAt,
}) = _AuthTokens;

bool get hasValidAccessToken => accessToken.isNotEmpty;
factory AuthTokens.fromJson(Map<String, dynamic> json) =>
_$AuthTokensFromJson(json);

bool get hasValidAccessToken => accessToken.isNotEmpty;
bool get hasValidRefreshToken => refreshToken.isNotEmpty;
bool get expiresSoon {
const minValidDifference = Duration(minutes: 1);

final now = expiresAt.isUtc ? DateTime.now().toUtc() : DateTime.now();

final lastValidTimestamp = now.subtract(minValidDifference);

return expiresAt.isAfter(lastValidTimestamp);
}
}
2 changes: 2 additions & 0 deletions lib/data/services/response_objects/tokens_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class TokensResponse with _$TokensResponse {
refreshToken: refreshToken,
tokenType: tokenType,
expiresIn: expiresIn,
expiresAt:
DateTime.now().toUtc().add(Duration(milliseconds: expiresIn.toInt())),
);
}
}

0 comments on commit 716cae3

Please sign in to comment.