Skip to content

Commit 9fadbaf

Browse files
authored
Add debug logging around token expiration (#7461)
* Add debug logging around token expiration. * Use FIRLogger instead of NSLog. * Move log statement. * Add another log statement when access token is updated.
1 parent 496432f commit 9fadbaf

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

FirebaseAuth/Sources/SystemService/FIRSecureTokenService.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
2020

2121
#import "FirebaseAuth/Sources/Auth/FIRAuthSerialTaskQueue.h"
22+
#import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
2223
#import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
2324
#import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
2425
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenRequest.h"
2526
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenResponse.h"
2627

28+
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
29+
2730
NS_ASSUME_NONNULL_BEGIN
2831

2932
/** @var kAPIKeyCodingKey
@@ -111,6 +114,7 @@ - (void)fetchAccessTokenForcingRefresh:(BOOL)forceRefresh
111114
complete();
112115
callback(self->_accessToken, nil, NO);
113116
} else {
117+
FIRLogDebug(kFIRLoggerAuth, @"I-AUT000017", @"Fetching new token from backend.");
114118
[self requestAccessToken:^(NSString *_Nullable token, NSError *_Nullable error,
115119
BOOL tokenUpdated) {
116120
complete();
@@ -189,6 +193,9 @@ - (void)requestAccessToken:(FIRFetchAccessTokenCallback)callback {
189193
self->_accessToken = [newAccessToken copy];
190194
self->_accessTokenExpirationDate = response.approximateExpirationDate;
191195
tokenUpdated = YES;
196+
FIRLogDebug(kFIRLoggerAuth, @"I-AUT000017",
197+
@"Updated access token. Estimated expiration date: %@, current date: %@",
198+
self->_accessTokenExpirationDate, [NSDate date]);
192199
}
193200
NSString *newRefreshToken = response.refreshToken;
194201
if (newRefreshToken.length && ![newRefreshToken isEqualToString:self->_refreshToken]) {
@@ -200,7 +207,19 @@ - (void)requestAccessToken:(FIRFetchAccessTokenCallback)callback {
200207
}
201208

202209
- (BOOL)hasValidAccessToken {
203-
return _accessToken && [_accessTokenExpirationDate timeIntervalSinceNow] > kFiveMinutes;
210+
BOOL hasValidAccessToken =
211+
_accessToken && [_accessTokenExpirationDate timeIntervalSinceNow] > kFiveMinutes;
212+
if (hasValidAccessToken) {
213+
FIRLogDebug(kFIRLoggerAuth, @"I-AUT000017",
214+
@"Has valid access token. Estimated expiration date: %@, current date: %@",
215+
_accessTokenExpirationDate, [NSDate date]);
216+
} else {
217+
FIRLogDebug(
218+
kFIRLoggerAuth, @"I-AUT000017",
219+
@"Does not have valid access token. Estimated expiration date: %@, current date: %@",
220+
_accessTokenExpirationDate, [NSDate date]);
221+
}
222+
return hasValidAccessToken;
204223
}
205224

206225
@end

FirebaseAuth/Sources/User/FIRUser.m

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -881,18 +881,22 @@ - (void)getIDTokenResultWithCompletion:(nullable FIRAuthTokenResultCallback)comp
881881
- (void)getIDTokenResultForcingRefresh:(BOOL)forceRefresh
882882
completion:(nullable FIRAuthTokenResultCallback)completion {
883883
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
884-
[self internalGetTokenForcingRefresh:forceRefresh
885-
callback:^(NSString *_Nullable token, NSError *_Nullable error) {
886-
FIRAuthTokenResult *tokenResult;
887-
if (token) {
888-
tokenResult = [FIRAuthTokenResult tokenResultWithToken:token];
889-
}
890-
if (completion) {
891-
dispatch_async(dispatch_get_main_queue(), ^{
892-
completion(tokenResult, error);
893-
});
894-
}
895-
}];
884+
[self
885+
internalGetTokenForcingRefresh:forceRefresh
886+
callback:^(NSString *_Nullable token, NSError *_Nullable error) {
887+
FIRAuthTokenResult *tokenResult;
888+
if (token) {
889+
tokenResult = [FIRAuthTokenResult tokenResultWithToken:token];
890+
FIRLogDebug(kFIRLoggerAuth, @"I-AUT000017",
891+
@"Actual token expiration date: %@, current date: %@",
892+
tokenResult.expirationDate, [NSDate date]);
893+
}
894+
if (completion) {
895+
dispatch_async(dispatch_get_main_queue(), ^{
896+
completion(tokenResult, error);
897+
});
898+
}
899+
}];
896900
});
897901
}
898902

0 commit comments

Comments
 (0)