Skip to content

Commit 39591c8

Browse files
committed
fix unnecessary popups
1 parent f95849f commit 39591c8

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

src/modules/authentication/AuthenticationWrapper.ts

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -146,68 +146,62 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
146146
}
147147

148148
public async getToken(): Promise<AuthenticationResult> {
149-
try {
150-
const account = this.getAccount();
151-
if (!account) {
152-
// If no account is found, try to get accounts from cache
153-
const allAccounts = msalApplication.getAllAccounts();
154-
if (allAccounts.length > 0) {
155-
this.storeHomeAccountId(allAccounts[0]);
156-
// Use the first account found
157-
const silentRequest: SilentRequest = {
158-
scopes: defaultScopes,
159-
authority: this.getAuthority(),
160-
account: allAccounts[0],
161-
redirectUri: getCurrentUri(),
162-
forceRefresh: false
163-
};
164-
165-
try {
166-
return await msalApplication.acquireTokenSilent(silentRequest);
167-
} catch (error) {
168-
// If silent token acquisition fails, fall through to interactive login
169-
throw new Error(`Silent token acquisition failed, attempting interactive login: ${error}`);
170-
}
149+
const account = this.getAccount();
150+
if (!account) {
151+
// If no active account, check cache without triggering interaction
152+
const allAccounts = msalApplication.getAllAccounts();
153+
if (allAccounts.length > 0) {
154+
// Try silent acquisition with the first cached account
155+
const silentRequest: SilentRequest = {
156+
scopes: defaultScopes,
157+
authority: this.getAuthority(),
158+
account: allAccounts[0],
159+
redirectUri: getCurrentUri(),
160+
forceRefresh: false
161+
};
162+
163+
try {
164+
// Attempt silent acquisition
165+
const result = await msalApplication.acquireTokenSilent(silentRequest);
166+
// If successful, store the account ID as it's now the active one
167+
this.storeHomeAccountId(result.account!);
168+
return result;
169+
} catch (error) {
170+
// If silent fails (e.g., requires interaction, expired), throw error.
171+
throw new Error(`Silent token acquisition failed for cached account: ${error}`);
171172
}
172-
173-
// If we get here, we need to prompt for login
174-
return await this.loginWithInteraction(defaultScopes);
173+
} else {
174+
// No active account and no cached accounts - user needs to log in explicitly.
175+
// Throw error indicating login is required
176+
throw new Error('No active or cached account found. User login required.');
175177
}
178+
}
176179

177-
// We have an account, try to get token silently
178-
const silentRequest: SilentRequest = {
179-
scopes: defaultScopes,
180-
authority: this.getAuthority(),
181-
account,
182-
redirectUri: getCurrentUri(),
183-
claims: this.claimsAvailable ? this.getClaims() : undefined,
184-
// Don't force refresh on first attempt
185-
forceRefresh: false
186-
};
180+
// We have an active account, try to get token silently
181+
const silentRequest: SilentRequest = {
182+
scopes: defaultScopes,
183+
authority: this.getAuthority(),
184+
account,
185+
redirectUri: getCurrentUri(),
186+
claims: this.claimsAvailable ? this.getClaims() : undefined,
187+
forceRefresh: false
188+
};
187189

188-
try {
189-
return await msalApplication.acquireTokenSilent(silentRequest);
190-
} catch (error) {
191-
if (error instanceof InteractionRequiredAuthError) {
192-
// Token expired or invalid, try with forceRefresh
193-
try {
194-
silentRequest.forceRefresh = true;
195-
return await msalApplication.acquireTokenSilent(silentRequest);
196-
} catch {
197-
// Falling back to interactive login as refresh token is expired or invalid
198-
return await this.loginWithInteraction(defaultScopes);
199-
}
200-
}
201-
throw error;
202-
}
190+
try {
191+
return await msalApplication.acquireTokenSilent(silentRequest);
203192
} catch (error) {
204-
// Only throw if it's not an InteractionRequiredAuthError
205-
if (!(error instanceof InteractionRequiredAuthError)) {
206-
throw error;
193+
if (error instanceof InteractionRequiredAuthError) {
194+
// Attempt silent refresh first
195+
try {
196+
silentRequest.forceRefresh = true;
197+
return await msalApplication.acquireTokenSilent(silentRequest);
198+
} catch (refreshError) {
199+
// If refresh also fails, throw error indicating interaction is needed.
200+
throw new Error(`Silent token refresh failed, login required: ${refreshError}`);
201+
}
207202
}
208-
209-
// For InteractionRequiredAuthError, attempt interactive login
210-
return await this.loginWithInteraction(defaultScopes);
203+
// Re-throw other unexpected silent errors so the caller can handle them
204+
throw error; // Removed console.error, just re-throw
211205
}
212206
}
213207

0 commit comments

Comments
 (0)