From 716cae319ca4aa398508398a97262f6a2d726638 Mon Sep 17 00:00:00 2001 From: Nikita Sirovskiy Date: Thu, 5 Jan 2023 16:30:26 +0300 Subject: [PATCH] chore(#80): Add expiresAt & expiresSoon to authTokens --- lib/data/model/auth/auth_tokens.dart | 15 ++++++++++++++- .../response_objects/tokens_response.dart | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/data/model/auth/auth_tokens.dart b/lib/data/model/auth/auth_tokens.dart index 6b1a461..f9f6a32 100644 --- a/lib/data/model/auth/auth_tokens.dart +++ b/lib/data/model/auth/auth_tokens.dart @@ -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 { @@ -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 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); + } } diff --git a/lib/data/services/response_objects/tokens_response.dart b/lib/data/services/response_objects/tokens_response.dart index a08f9e0..c7c153b 100644 --- a/lib/data/services/response_objects/tokens_response.dart +++ b/lib/data/services/response_objects/tokens_response.dart @@ -35,6 +35,8 @@ class TokensResponse with _$TokensResponse { refreshToken: refreshToken, tokenType: tokenType, expiresIn: expiresIn, + expiresAt: + DateTime.now().toUtc().add(Duration(milliseconds: expiresIn.toInt())), ); } }