11import { version } from '../package.json' ;
2- import { OptoutIdentity , Identity , isOptoutIdentity } from './Identity' ;
2+ import { OptoutIdentity , Identity , isOptoutIdentity , isValidIdentity } from './Identity' ;
33import { IdentityStatus , InitCallbackManager } from './initCallbacks' ;
44import { SdkOptions , isSDKOptionsOrThrow } from './sdkOptions' ;
55import { Logger , MakeLogger } from './sdk/logger' ;
@@ -163,9 +163,8 @@ export abstract class SdkBase {
163163
164164 public getIdentity ( ) : Identity | null {
165165 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 ;
169168 }
170169
171170 // When the SDK has been initialized, this function should return the token
@@ -181,11 +180,23 @@ export abstract class SdkBase {
181180 }
182181
183182 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+ }
184188 return ! this . isIdentityAvailable ( ) ;
185189 }
186190
187191 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+ ) ;
189200 }
190201
191202 public hasOptedOut ( ) {
@@ -230,10 +241,14 @@ export abstract class SdkBase {
230241 this . _initCallbackManager ?. addInitCallback ( opts . callback ) ;
231242 }
232243
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+
237252 if ( useNewIdentity || opts . callback ) {
238253 let identity = useNewIdentity ? opts . identity : previousOpts . identity ?? null ;
239254 if ( identity ) {
@@ -280,11 +295,6 @@ export abstract class SdkBase {
280295 if ( this . hasOptedOut ( ) ) this . _callbackManager . runCallbacks ( EventType . OptoutReceived , { } ) ;
281296 }
282297
283- private isIdentityValid ( ) {
284- const identity = this . _identity ?? this . getIdentityNoInit ( ) ;
285- return identity && ! hasExpired ( identity . refresh_expires ) ;
286- }
287-
288298 private temporarilyUnavailable ( identity : Identity | OptoutIdentity | null | undefined ) {
289299 if ( ! identity && this . _apiClient ?. hasActiveRequests ( ) ) return true ;
290300 // returns true if identity is expired but refreshable
0 commit comments