@@ -102,10 +102,14 @@ export class PublicClientApplication {
102
102
// Initialize default authority instance
103
103
TrustedAuthority . setTrustedAuthoritiesFromConfig ( this . config . auth . knownAuthorities , this . config . auth . cloudDiscoveryMetadata ) ;
104
104
105
- this . defaultAuthorityPromise = AuthorityFactory . createDiscoveredInstance (
106
- this . config . auth . authority ,
107
- this . networkClient
108
- ) ;
105
+ this . defaultAuthorityPromise = AuthorityFactory . createDiscoveredInstance ( this . config . auth . authority , this . networkClient ) ;
106
+
107
+ const { location : { hash } } = window ;
108
+ const cachedHash = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) , CacheSchemaType . TEMPORARY ) as string ;
109
+ if ( StringUtils . isEmpty ( hash ) && StringUtils . isEmpty ( cachedHash ) ) {
110
+ // There is no hash - assume we are in clean state and clear any current request data.
111
+ this . browserStorage . cleanRequest ( ) ;
112
+ }
109
113
}
110
114
111
115
// #region Redirect Flow
@@ -156,19 +160,19 @@ export class PublicClientApplication {
156
160
*/
157
161
private async handleRedirectResponse ( ) : Promise < AuthenticationResult > {
158
162
// Get current location hash from window or cache.
159
- const { location : { hash} } = window ;
163
+ const { location : { hash } } = window ;
160
164
const cachedHash = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) , CacheSchemaType . TEMPORARY ) as string ;
161
165
const isResponseHash = UrlString . hashContainsKnownProperties ( hash ) ;
162
166
const loginRequestUrl = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . ORIGIN_URI ) , CacheSchemaType . TEMPORARY ) as string ;
163
167
const currentUrl = BrowserUtils . getCurrentUri ( ) ;
164
- if ( loginRequestUrl === currentUrl ) {
168
+ if ( loginRequestUrl === currentUrl || ! this . config . auth . navigateToLoginRequestUrl ) {
165
169
// We don't need to navigate - check for hash and prepare to process
166
170
if ( isResponseHash ) {
167
171
BrowserUtils . clearHash ( ) ;
168
172
return this . handleHash ( hash ) ;
169
173
} else {
170
174
// Loaded page with no valid hash - pass in the value retrieved from cache, or null/empty string
171
- return this . handleHash ( ` ${ cachedHash } ` ) ;
175
+ return this . handleHash ( cachedHash ) ;
172
176
}
173
177
}
174
178
@@ -184,39 +188,27 @@ export class PublicClientApplication {
184
188
// Navigate to target url
185
189
BrowserUtils . navigateWindow ( loginRequestUrl , true ) ;
186
190
}
187
- return null ;
188
- }
189
-
190
- if ( ! isResponseHash ) {
191
- // Loaded page with no valid hash - pass in the value retrieved from cache, or null/empty string
192
- return this . handleHash ( cachedHash ) ;
193
- }
194
-
195
- if ( ! this . config . auth . navigateToLoginRequestUrl ) {
196
- // We don't need to navigate - check for hash and prepare to process
197
- BrowserUtils . clearHash ( ) ;
198
- return this . handleHash ( hash ) ;
199
191
}
200
192
201
193
return null ;
202
194
}
203
195
204
196
/**
205
- * Checks if hash exists and handles in window. Otherwise, cancel any current requests and continue.
197
+ * Checks if hash exists and handles in window.
206
198
* @param responseHash
207
199
* @param interactionHandler
208
200
*/
209
201
private async handleHash ( responseHash : string ) : Promise < AuthenticationResult > {
202
+ // There is no hash - return null.
203
+ if ( StringUtils . isEmpty ( responseHash ) ) {
204
+ return null ;
205
+ }
206
+
207
+ // Hash contains known properties - handle and return in callback
210
208
const currentAuthority = this . browserStorage . getCachedAuthority ( ) ;
211
209
const authClient = await this . createAuthCodeClient ( currentAuthority ) ;
212
210
const interactionHandler = new RedirectHandler ( authClient , this . browserStorage ) ;
213
- if ( ! StringUtils . isEmpty ( responseHash ) ) {
214
- // Hash contains known properties - handle and return in callback
215
- return interactionHandler . handleCodeResponse ( responseHash , this . browserCrypto ) ;
216
- }
217
- // There is no hash - assume we are in clean state and clear any current request data.
218
- this . browserStorage . cleanRequest ( ) ;
219
- return null ;
211
+ return interactionHandler . handleCodeResponse ( responseHash , this . browserCrypto ) ;
220
212
}
221
213
222
214
/**
@@ -448,6 +440,7 @@ export class PublicClientApplication {
448
440
// Handle response from hash string.
449
441
return await silentHandler . handleCodeResponse ( hash ) ;
450
442
} catch ( e ) {
443
+ this . browserStorage . cleanRequest ( ) ;
451
444
throw e ;
452
445
}
453
446
}
0 commit comments