1
1
import { version } from '../package.json' ;
2
- import { OptoutIdentity , Identity , isOptoutIdentity } from './Identity' ;
2
+ import { OptoutIdentity , Identity , isOptoutIdentity , isValidIdentity } from './Identity' ;
3
3
import { IdentityStatus , InitCallbackManager } from './initCallbacks' ;
4
4
import { SdkOptions , isSDKOptionsOrThrow } from './sdkOptions' ;
5
5
import { Logger , MakeLogger } from './sdk/logger' ;
@@ -163,9 +163,8 @@ export abstract class SdkBase {
163
163
164
164
public getIdentity ( ) : Identity | null {
165
165
const identity = this . _identity ?? this . getIdentityNoInit ( ) ;
166
- return identity && ! this . temporarilyUnavailable ( identity ) && ! isOptoutIdentity ( identity )
167
- ? identity
168
- : null ;
166
+ // if identity is valid (includes not opted out) and available, return it
167
+ return isValidIdentity ( identity ) && ! this . temporarilyUnavailable ( identity ) ? identity : null ;
169
168
}
170
169
171
170
// When the SDK has been initialized, this function should return the token
@@ -181,11 +180,23 @@ export abstract class SdkBase {
181
180
}
182
181
183
182
public isLoginRequired ( ) {
183
+ const identity = this . _identity ?? this . getIdentityNoInit ( ) ;
184
+ // if identity temporarily unavailable, login is not required
185
+ if ( this . temporarilyUnavailable ( identity ) ) {
186
+ return false ;
187
+ }
184
188
return ! this . isIdentityAvailable ( ) ;
185
189
}
186
190
187
191
public isIdentityAvailable ( ) {
188
- return this . isIdentityValid ( ) || this . _apiClient ?. hasActiveRequests ( ) ;
192
+ const identity = this . _identity ?? this . getIdentityNoInit ( ) ;
193
+ // available if identity exists and has not expired or if there active requests
194
+ return (
195
+ ( identity &&
196
+ ! hasExpired ( identity . refresh_expires ) &&
197
+ ! this . temporarilyUnavailable ( identity ) ) ||
198
+ this . _apiClient ?. hasActiveRequests ( )
199
+ ) ;
189
200
}
190
201
191
202
public hasOptedOut ( ) {
@@ -230,10 +241,14 @@ export abstract class SdkBase {
230
241
this . _initCallbackManager ?. addInitCallback ( opts . callback ) ;
231
242
}
232
243
233
- const useNewIdentity =
234
- opts . identity &&
235
- ( ! previousOpts . identity ||
236
- opts . identity . identity_expires > previousOpts . identity . identity_expires ) ;
244
+ let useNewIdentity ;
245
+ if ( ! opts . identity ) useNewIdentity = true ;
246
+ else {
247
+ useNewIdentity =
248
+ ! previousOpts . identity ||
249
+ opts . identity . identity_expires > previousOpts . identity . identity_expires ;
250
+ }
251
+
237
252
if ( useNewIdentity || opts . callback ) {
238
253
let identity = useNewIdentity ? opts . identity : previousOpts . identity ?? null ;
239
254
if ( identity ) {
@@ -280,11 +295,6 @@ export abstract class SdkBase {
280
295
if ( this . hasOptedOut ( ) ) this . _callbackManager . runCallbacks ( EventType . OptoutReceived , { } ) ;
281
296
}
282
297
283
- private isIdentityValid ( ) {
284
- const identity = this . _identity ?? this . getIdentityNoInit ( ) ;
285
- return identity && ! hasExpired ( identity . refresh_expires ) ;
286
- }
287
-
288
298
private temporarilyUnavailable ( identity : Identity | OptoutIdentity | null | undefined ) {
289
299
if ( ! identity && this . _apiClient ?. hasActiveRequests ( ) ) return true ;
290
300
// returns true if identity is expired but refreshable
0 commit comments